gpt4 book ai didi

android - 无法保存裁剪后的图像 - Android

转载 作者:行者123 更新时间:2023-11-30 02:33:43 26 4
gpt4 key购买 nike

这是我从 here 得到的代码

    public class CatFragment extends Fragment implements OnClickListener{

private DBCreater dbCreate;

private static final int SELECT_PICTURE = 1;

private String selectedImagePath = "android.resource://com.example.abcd/" + R.drawable.pets;
private ImageView img;
//private Button imgBtn;

private Uri mCropImagedUri;

@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) {
try {
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", false);

File f = createNewFile("CROP_");
try {
f.createNewFile();
} catch (IOException ex) {
Log.e("io", ex.getMessage());
}

mCropImagedUri = Uri.fromFile(f);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
mCropImagedUri);
// start the activity - we handle returning in
// onActivityResult
startActivityForResult(intent, SELECT_PICTURE);
} catch (ActivityNotFoundException anfe) {
// display an error message

}
}
});

return gv;

}


private File createNewFile(String prefix){
if(prefix==null || "".equalsIgnoreCase(prefix)){
prefix="IMG_";
}
File newDirectory = new File(Environment.getExternalStorageDirectory()+"/Android/data/com.example.abcd/files/");
if(!newDirectory.exists()){
if(newDirectory.mkdir()){
Log.d(this.getClass().getName(), newDirectory.getAbsolutePath()+" directory created");
}
}
File file = new File(newDirectory,(prefix+System.currentTimeMillis()+".jpg"));
if(file.exists()){
//this wont be executed
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

return file;
}


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); //To get image path
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);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//access the database
dbCreate = new DBCreater(getActivity());

}



@Override
public void onClick(View v) {
displayNextAlert();

}

通过使用此代码,我可以裁剪图像,但它不会保存并返回 Toast 消息统计信息无法保存裁剪后的图像。我在 LogCat 中看不到任何其他错误。请有人帮忙?提前致谢。

LogCat:

11-15 07:15:38.215: E/AndroidRuntime(27214): FATAL EXCEPTION: main
11-15 07:15:38.215: E/AndroidRuntime(27214): java.lang.RuntimeException: Unable to resume activity {com.example.abcd/com.example.abcd.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.abcd/com.example.abcd.MainActivity}: java.lang.NullPointerException
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2919)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2948)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2354)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.app.ActivityThread.access$700(ActivityThread.java:159)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.os.Looper.loop(Looper.java:176)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.app.ActivityThread.main(ActivityThread.java:5419)
11-15 07:15:38.215: E/AndroidRuntime(27214): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 07:15:38.215: E/AndroidRuntime(27214): at java.lang.reflect.Method.invoke(Method.java:525)
11-15 07:15:38.215: E/AndroidRuntime(27214): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
11-15 07:15:38.215: E/AndroidRuntime(27214): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
11-15 07:15:38.215: E/AndroidRuntime(27214): at dalvik.system.NativeStart.main(Native Method)
11-15 07:15:38.215: E/AndroidRuntime(27214): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.abcd/com.example.abcd.MainActivity}: java.lang.NullPointerException
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.app.ActivityThread.deliverResults(ActivityThread.java:3500)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2906)
11-15 07:15:38.215: E/AndroidRuntime(27214): ... 12 more
11-15 07:15:38.215: E/AndroidRuntime(27214): Caused by: java.lang.NullPointerException
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1147)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.content.ContentResolver.query(ContentResolver.java:401)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.content.ContentResolver.query(ContentResolver.java:360)
11-15 07:15:38.215: E/AndroidRuntime(27214): at com.example.abcd.NewPetsFragment.getPath(NewPetsFragment.java:155)
11-15 07:15:38.215: E/AndroidRuntime(27214): at com.example.abcd.NewPetsFragment.onActivityResult(NewPetsFragment.java:143)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.app.Activity.dispatchActivityResult(Activity.java:5567)
11-15 07:15:38.215: E/AndroidRuntime(27214): at android.app.ActivityThread.deliverResults(ActivityThread.java:3496)
11-15 07:15:38.215: E/AndroidRuntime(27214): ... 13 more

最佳答案

我通过如下更改保存的目录路径使代码正常工作:

 File newDirectory = new File(Environment.getExternalStorageDirectory()+"/test/");

原因:我无法保存到写保护目录“/Android/data/com.example.abcd/files/”

并通过对 onActivityResult 进行以下更改:

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); //To get image path

selectedImagePath = mCropImagedUri.getPath(); //To get image path

System.out.println("Image Path : " + selectedImagePath);
// img.setImageURI(selectedImageUri);


}
}
}

注意:您可能不需要注释掉 img.setImageURI 行。我必须出于测试目的。

确保你在AndroidManifest中添加了写权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - 无法保存裁剪后的图像 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26915437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com