我的应用中的用户可以选择拍摄图像/从图库中选择,然后裁剪图像。在大多数设备上一切正常,但有些裁剪失败,并且无法保存裁剪的照片"消息出现(我知道这发生在HTC上的一个m8我手边没有这个设备来检查它)。我正在使用xamarin进行开发,但它基本上与java中的代码相同。
以下是用于从图库中选择的代码:
protected void ChooseFromGallery ()
{
Intent intent = new Intent (Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
intent.SetType ("image/*");
StartActivityForResult (intent, ChooseImageCode);
}
此代码处理结果:
if (requestCode == ChooseImageCode && resultCode == Result.Ok)
{
Uri selectedImage = data.Data;
var input = this.ContentResolver.OpenInputStream (selectedImage);
SharedContext.Bitmap = BitmapFactory.DecodeStream (input);
CropImage (selectedImage);
}
这是裁剪图像方法:
protected void CropImage (Uri imageUri)
{
Intent intent = new Intent ("com.android.camera.action.CROP");
intent.SetType ("image/*");
_targetUri = FileUtils.GetTempUri ();
IList<ResolveInfo> list = PackageManager.QueryIntentActivities (intent, 0);
int size = list.Count;
if (size == 0)
{
Toast.MakeText (this, "Can not find image crop app", ToastLength.Short).Show ();
return;
}
else
{
intent.SetData (imageUri);
intent.PutExtra ("outputX", 640);
intent.PutExtra ("outputY", 640);
intent.PutExtra ("aspectX", 1);
intent.PutExtra ("aspectY", 1);
intent.PutExtra ("output", _targetUri);
intent.PutExtra ("outputFormat", Bitmap.CompressFormat.Png);
intent.PutExtra ("scale", true);
if (size >= 1)
{
Intent i = new Intent (intent);
ResolveInfo res = list [0];
i.SetComponent (new ComponentName (res.ActivityInfo.PackageName, res.ActivityInfo.Name));
StartActivityForResult (i, CropPictureRequestCode);
}
}
}
我有没有想念。
答案 0 :(得分:0)
我一直在开发一款可以裁剪屏幕截图的应用:
**within fragment**
// get screen shot
Bitmap bmp = screenShot(v);
bmp = Bitmap.createBitmap(bmp, 0, 0, v.getWidth(), 450);
调用保存的方法:
// SAVE SCREEN SHOT TO INTERNAL STORAGE AND RETURN PATH TO IMAGE
// ****************************************************************
private String saveToInternalStorage(View v, Bitmap bitmapImage) {
ContextWrapper cw = new ContextWrapper(v.getContext());
TinyDB tinydb = new TinyDB(v.getContext());
int counter = tinydb.getInt("counter"); // get workout #
String imageDir = weekWorkout[counter];
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir(imageDir, Context.MODE_PRIVATE);
// Create imageDir
File mypath = new File(directory, imageDir + ".jpg");
ArrayList<String> imageAddressJpgList = new ArrayList<String>();
imageAddressJpgList = tinydb.getList("imageAddressJpgList");
imageAddressJpgList.add(counter, imageDir + ".jpg");
tinydb.putList("imageAddressJpgList", imageAddressJpgList);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to
// the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return directory.getAbsolutePath();
}
// TAKES SCREEN SHOT
// ********************
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
TinyDB是我在git btw上找到的一个已保存的参考魔术课。
我怀疑这会有所帮助,但我只是觉得我想开始参与。