根据我之前问题的答案,我成功地将图片从图库加载到表单上的 imageview
,没有任何错误。现在我想将图像压缩到 150KB 并在提交表单时将其作为 PNG 格式 存储在应用程序缓存中。同时我想将图片url存储在SQLite中。我认为这可以通过将图像名称放入隐藏的 TextView 或编辑 View 中来完成。但是我没有任何线索可以压缩图像并将其存储在缓存中。以下是到目前为止所做的一切的代码。请有人帮忙。
public class NewPetsFragment extends Fragment implements OnClickListener{
private DBCreater dbCreate;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
private static final String cacheDir = "/Android/data/com.example.pet/cache/";
private static final String CACHE_FILENAME = ".cache";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View gv = inflater.inflate(R.layout.new_pet, null);
Spinner sp = (Spinner) gv.findViewById(R.id.ETPetType);
// get reference
sp.setAdapter(new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_spinner_dropdown_item, petType));
Button btnSubmit = (Button) gv.findViewById(R.id.ButtonNext);
btnSubmit.setOnClickListener(this);
img = (ImageView)gv.findViewById(R.id.ImageView01);
((Button) gv.findViewById(R.id.ETPetImg))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
return gv;
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
//Cursor cursor = managedQuery(uri, projection, null, null, null);
Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
注意:此代码仅用于图片加载
更新: 我像这样从图库中添加一些裁剪选项
img = (ImageView)gv.findViewById(R.id.ImageView01);
((Button) gv.findViewById(R.id.ETPetImg))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
return gv;
现在应用程序在裁剪后崩溃并返回错误,因为无法恢复 Activity 。我该如何解决这个问题?
最佳答案
如果您希望将图像压缩到 150K 字节或更小,并且图像可以是多种格式,我建议如下:
- 将图像转换为 JPEG(如果尚未转换)。
- 如果图片小于 150K,您就完成了。
- 如果不是,则对 JPEG 图像进行缩减采样,以使其占用更少的空间。或者,如果图片很大(可能是 1024 x 1024),请减小图片本身的尺寸。
这里有一些 Java 代码链接。我还找到了指向其他语言代码的链接。
图像转换:code
下采样:code
只需减小图像大小:programs
关于android - 如何在android中压缩和存储图库中的图像到应用程序缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26768852/