javascript - 捕获图像后网格视图不会自动刷新
问题描述
我尝试创建一个应用程序来捕获图像并保存到特定文件夹,它会在网格视图中显示拍摄的照片。但是在捕获图像后,它不会立即反映在网格视图中,而是在进行下一次捕获后它会更新。请帮助在捕获后立即更新网格视图中的捕获图像。还请帮助,如何将捕获的图像大小限制为最大 300 kb 大小,应保存在特定文件夹中。
package com.example.surveyassist;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class CaptureActivity extends AppCompatActivity {
private static final int IMAGE_REQUEST = 1;
private DateFormat dateFormata = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentImagePath =null;
final int CAMERA_CAPTURE = 1;
private GridView grid;
private List<String> listOfImagesPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_capture);
grid = ( GridView) findViewById(R.id.gridviewimg);
listOfImagesPath = null;
listOfImagesPath = RetriveCapturedImagePath();
if(listOfImagesPath!=null) {
grid.setAdapter(new ImageListAdapter(this, listOfImagesPath));
}
nxtbuttonset();
}
public void captureImage(View view) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager())!=null)
{
File imageFile = null;
try {
imageFile = getImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (imageFile !=null)
{
Uri imageUri = FileProvider.getUriForFile(this, "com.example.surveyassist.fileprovider", imageFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, IMAGE_REQUEST);
}
}
}
private File getImageFile() throws IOException {
final String folderpath = getIntent().getExtras().getString("key");
String timeStamp = dateFormata.format(new Date());
String imageName = timeStamp;
String _path = folderpath +File.separator + timeStamp+".jpg";
File imageFile = new File (_path);
currentImagePath=imageFile.getAbsolutePath();
return imageFile;
}
public void nxtbuttonset() {
Button buttonnext = (Button) findViewById(R.id.buttonNexta);
buttonnext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent intenta = new Intent(CaptureActivity.this, ZipActivity.class);
//startActivity(intenta);
moveTaskToBack(true);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
}
//user is returning from capturing an image using the camera\
public void retrive() {
Bitmap bitmap = null;
final String folderpath = getIntent().getExtras().getString("key");
String timeStamp = dateFormata.format(new Date());
String imageName = timeStamp;
String _path = folderpath + File.separator + timeStamp + ".jpg";
File f = new File(_path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
try {
bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
FileOutputStream out = new FileOutputStream(_path);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
} catch (FileNotFoundException e) {
e.getMessage();
} catch (IOException e) {
e.printStackTrace();
}
listOfImagesPath = null;
listOfImagesPath = RetriveCapturedImagePath();
if (listOfImagesPath != null) {
grid.setAdapter(new ImageListAdapter(this, listOfImagesPath));
}
}
private List<String> RetriveCapturedImagePath() {
final String folderpath = getIntent().getExtras().getString("key");
List<String> tFileList = new ArrayList<String>();
File f = new File(folderpath);
if (f.exists()) {
File[] files=f.listFiles();
Arrays.sort(files);
for(int i=0; i<files.length; i++){
File file = files[i];
if(file.isDirectory())
continue;
tFileList.add(file.getPath());
}
}
return tFileList;
}
public class ImageListAdapter extends BaseAdapter
{
private Context context;
private List<String> imgPic;
public ImageListAdapter(Context c, List<String> thePic)
{
context = c;
imgPic = thePic;
}
public int getCount() {
if(imgPic != null)
return imgPic.size();
else
return 0;
}
//—returns the ID of an item—
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//—returns an ImageView view—
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inDither=false; //Disable Dithering mode
bfOptions.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage=new byte[32 * 1024];
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
FileInputStream fs = null;
Bitmap bm;
try {
fs = new FileInputStream(new File(imgPic.get(position).toString()));
if(fs!=null) {
bm=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
imageView.setImageBitmap(bm);
imageView.setId(position);
imageView.setLayoutParams(new GridView.LayoutParams(500, 400));
}
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fs!=null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return imageView;
}
}
}
布局代码是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CaptureActivity">
<RelativeLayout
android:id="@+id/RelativeGridLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<GridView
android:id="@+id/gridviewimg"
android:layout_width="match_parent"
android:layout_height="620dp"
android:gravity="center"
android:numColumns="2"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:scrollbarStyle="outsideInset"
android:smoothScrollbar="true"
android:verticalSpacing="10dp"></GridView>
</RelativeLayout>
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="57dp"
android:layout_alignBottom="@+id/RelativeGridLayout"
android:layout_marginBottom="0dp">
<ImageButton
android:id="@+id/imageButtonCam"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="captureImage"
app:srcCompat="@drawable/camerabutton" />
<Button
android:id="@+id/buttonNexta"
android:layout_width="82dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:background="@drawable/btn"
android:text="Exit"
android:textSize="19dp" />
</RelativeLayout>
解决方案
推荐阅读
- excel - 我正在尝试更改多个工作簿的事件过程?
- azure - 将数据写入 Azure Data Lake Store - Powershell 脚本
- css - CSS改变反应MUI选择
- algorithm - 树证书中的子序列是否保证它包含给定的树?
- c# - 无法使用具有应用程序权限的 Microsoft Graph 获取用户信息
- c - 即使条件为假,while循环也会继续运行
- asp.net-core - NetCore 3.1 PostAsync CustomHeaders 不工作
- reactjs - 没有找到匹配的文件...。(反应JS)
- mysql - 如何远程或通过 IP 地址连接到 mariaDB?
- python - 多个字符串输入 - Python