我的图书馆项目中有一项活动。从那个活动,我试图从图书馆获取图像。千分之一(隐喻),下面的代码工作正常。但大多数情况下,在选择图像后,应用程序会陷入黑屏。
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/* video/*");
startActivityForResult(intent, REQUEST_GALLERY);
LOG
这是成功的场景:
06-22 11:22:16.559 19038-19038/com.xyz D/SELECT_IMAGE: onCreate
06-22 11:22:16.869 19038-19038/com.xyz D/SELECT_IMAGE: onResume
06-22 11:22:24.949 19038-19038/com.xyz D/SELECT_IMAGE: Before startActivityForResult
06-22 11:22:24.959 19038-19038/com.xyz D/SELECT_IMAGE: After startActivityForResult
06-22 11:22:32.359 19038-19038/com.xyz D/SELECT_IMAGE: RESULT onActivityResult
06-22 11:22:32.429 19038-19038/com.xyz D/SELECT_IMAGE: onResume
在此之后,我再次尝试,然后只是这两个日志,然后卡住
06-22 11:23:02.919 19038-19038/com.xyz D/SELECT_IMAGE: Before startActivityForResult
06-22 11:23:02.929 19038-19038/com.xyz D/SELECT_IMAGE: After startActivityForResult
已编辑:登录adb shell dumpsys activity top
java.lang.IndexOutOfBoundsException: Invalid index 6, size is 3
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.android.server.am.ActivityStack.getDumpActivitiesLocked(ActivityStack.java:6544)
at com.android.server.am.ActivityStackSupervisor.getDumpActivitiesLocked(ActivityStackSupervisor.java:5734)
at com.android.server.am.ActivityManagerService.dumpActivity(ActivityManagerService.java:17777)
at com.android.server.am.ActivityManagerService.dump(ActivityManagerService.java:16936)
at android.os.Binder.dump(Binder.java:334)
at android.os.Binder.onTransact(Binder.java:292)
at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:2568)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3242)
at android.os.Binder.execTransact(Binder.java:461)
答案 0 :(得分:-1)
在像此一样的activityResult上使用它
public class MainActivity extends AppCompatActivity {
final static int REQUEST_GALLERY=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/* video/*");
startActivityForResult(intent, REQUEST_GALLERY);
}
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
Toast.makeText(getApplicationContext(),"Recieved",Toast.LENGTH_LONG).show();
if (requestCode==REQUEST_GALLERY)
{
if(responseCode==RESULT_OK)
{
Uri selectedImage = intent.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.img_view);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
}