- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此代码允许用户在他的画廊中选择一个图像并裁剪它以将其放入 ImageView 中。但是每次我离开应用程序并再次打开它时,图像都不再存在,所以我想将 uri 保存为用户在共享首选项中裁剪的图像,以便它持久存在。
请像实现 cele 一样!
选择图像:
permissionsCheck();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
裁剪图片:
private void doCrop() {
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
if (size == 0) {
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return;
} else {
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", false);
intent.putExtra("return-data", true);
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
} else {
for (ResolveInfo res : list) {
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
co.appIntent= new Intent(intent);
co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) {
startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
}
});
builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
@Override
public void onCancel( DialogInterface dialog ) {
if (mImageCaptureUri != null ) {
getContentResolver().delete(mImageCaptureUri, null, null );
mImageCaptureUri = null;
}
}
} );
AlertDialog alert = builder.create();
alert.show();
}
}
}
Activity 结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
switch (requestCode) {
case PICK_IMAGE:
mImageCaptureUri = data.getData();
doCrop();
break;
case CROP_FROM_CAMERA:
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
imageView.setImageBitmap(photo);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 100000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
photo.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("image", String.valueOf(mImageCaptureUri));
editor.commit();
imageView.setImageURI(mImageCaptureUri);
imageView.invalidate();
}
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) f.delete();
break;
}
}
最佳答案
请不要将图像保存到 Preferences
中,它不是大数据 block 的存储空间,将裁剪图像以 .jpg 格式保存到数据文件夹中或 .png 图像,并将 URI 或路径存储到您的首选项中的裁剪图像文件。
然后您将能够检查路径是否存储在首选项中,并在应用程序启动时加载裁剪图像。
好的,我已经再次检查了您的源代码。在 onActivityResult()
中,您有一个变量 mImageCaptureUri
- 将此变量保存到 Preferences
中。当您重新启动您的应用程序时,使用该变量访问图像并以与裁剪原始图像相同的方式再次裁剪它。
关于android - 在共享首选项中选择、裁剪和保存 uri 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51113595/
我的组件的模板呈现一个列表: {{ m.name }} 组件为其设置动画 @Component({ selector: 'app-client-detail', tem
我在回收站 View 中为每个项目使用卡片 View ,但我在图片下方和上方有空白,我不确定如何,我需要摆脱它。这是每个项目的 xml 布局。
我有一个包含许多 float 子元素的 div。每个子项都是一个包含输入和标签的 div。在 Firefox 中它看起来不错,但在 IE7 中,一行的最后一项被换行以适合,而不是被移动到下一行。 Wr
我创建了一个包含三个按钮的 wxpython 框架,在将视频格式化到 wxpanel 上时出现问题 这就是我流式传输网络摄像头的方式。 #code for the stream of webcam t
我想为 ListView 中的每个项目创建一个 fragment ,因为我想分离出一些逻辑。我正在为每个项目使用 View 持有者。如果 View 不存在,我将创建一个新 fragment 并将其添加
我正在使用 Joda 解析 RSS 项中的 pubDate。日期必须采用 RFC-822 格式: http://feed2.w3.org/docs/error/InvalidRFC2822Date.h
我正在尝试为我的第一个项目提供一个在此循环中处于事件状态的类。但是第二个和第三个项目可能没有“事件”类。 @foreach ($news as $newsitem)
我是一名优秀的程序员,十分优秀!