我在Android应用中从图库加载图片时遇到问题。我有一些代码可以在模拟器中选择一个图像。当我做出选择时,抛出并捕获异常,意味着图像没有加载,但我不确定如何诊断正在发生的事情。
这是我的java代码文件:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
galleryImage = (ImageView) findViewById(R.id.imageView);
pick_btn = (Button)findViewById(R.id.button);
pick_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,RESULT_LOAD_IMG);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
Log.i("Image Column Index..", "hoiiii");
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
galleryImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}catch (Exception e)
{
Toast.makeText(getApplicationContext(),"You haven't picked any image",Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:1)
您需要先询问WRITE_EXTERNAL_STORAGE的运行时权限 你从gallary中选择图像。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
声明权限和请求代码
private static final String[] REQUEST_PERMISSIONS = new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private static final int REQUEST_PERMISSION_CODE = 007;
//check if permissions are needed
if (!checkPermission()) {
requestPermission();
} else {
//open gallary and pick image
}
private boolean checkPermission () {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
for (String permission : REQUEST_PERMISSIONS) {
if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
private void requestPermission () {
ActivityCompat.requestPermissions(this, REQUEST_PERMISSIONS, REQUEST_PERMISSION_CODE);
}
@Override
public void onRequestPermissionsResult ( int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults){
if (requestCode == REQUEST_PERMISSION_CODE) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//open gallary and pick image
}
}
}
答案 1 :(得分:0)
请检查清单文件中的用户权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 2 :(得分:0)
这是我编辑的代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (!Settings.System.canWrite(this) && (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data))
{
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, 2909);
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
} catch (Exception e)
{
Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_LONG).show();
}
}