从图库,Android工作室中选择时,不显示SD卡照片,

时间:2018-04-23 19:32:17

标签: android image-gallery android-external-storage

我正在使用此代码从图库中选择图像并将其显示在imageView中,这个问题仅适用于内部存储中的照片,当我从SD卡中选择图像时,它不是显示在我的imageView

我希望你有一个解决方案。

self.view.addSubview(self.toolBar)

}

我在清单文件中使用这些权限:

public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img = findViewById(R.id.imageView) ;
    btn = findViewById(R.id.button) ;
    btn.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            openGallery() ;
        }
    });
}
public void openGallery(){
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
    startActivityForResult(intent,PICK_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK &&  requestCode==PICK_IMAGE && data != null){
        imageUri = data.getData() ;


        img.setImageURI(imageUri);
    }else{
        Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:0)

我问我会回答,问题是我的大多数SD卡照片都是2.0 MB或更多而且它们不能直接由imageView处理,所以我在onActivityResult()中添加了一些行

public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       img = findViewById(R.id.imageView) ;
       btn = findViewById(R.id.button) ;
       btn.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
        openGallery() ;
    }
});
}
public void openGallery(){
        Intent intent = new Intent(Intent.ACTION_PICK, 
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
        startActivityForResult(intent,PICK_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
 data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK &&  requestCode==PICK_IMAGE && data != null){
    imageUri = data.getData() ;
    Bitmap bitmap  ;
     try {
       bitmap=MediaStore.Images.Media
                   .getBitmap(this.getContentResolver(),imgUri);
       Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, 160, 160, true);
       img.setImageBitmap(mBitmap);
     } catch (IOException e) {
            e.printStackTrace();
        }


}else{
    Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}

我希望这对那些能够面对同样问题的人有所帮助