2014-01-15 58 views
6

我有一个应用程序,其中有一个按钮,从图库中的照片,并能正常工作,并选择图像后,我的应用程序节目又重新回到了活动并显示图像中的图像视图。采摘从画廊和展示照片的图像视图

每一个工作正常,但有时,当我选择一些特定的图像预览没有显示。我也曾尝试仍压缩图像它不工作

我的代码如下.. onCreate()

galeryBtn=(Button)findViewById(R.id.buttonGallery); 
galeryBtn.setOnClickListener(new 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_IMAGE); 

    } 
}); 

在onActivityResult(INT requestCode,INT resultCode为,意图数据)

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
    Uri selectedImage = data.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(); 
    // String picturePath contains the path of selected Image 

    // Show the Selected Image on ImageView 
    ImageView imageView = (ImageView) findViewById(R.id.imgView); 
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

} 

回答

5

我遇到这样从资源获取光标URI,开流,设置类似的问题位图等,而且它一直都有错误。

所以我搜索库,发现图像选择器,库库。

我敢打赌,你想从image-chooser-library

这是非常容易使用,解决了所有这些细节问题的问题对你来说,就像从Picasa等图像

希望这是有益的尝试这一项目您。

+0

谢谢妳,还是工作我已经在不同的手机 – Biplab

+0

测试我已经测试了,但是当我从电话给它toest选择一些图片“文件未找到” – Biplab

+0

你选择高亮图像?是这样的,检查出dev_get_content分支,看看它是否工作。 –

3

您试图在onActivityResult()中加载位图的方式并不完全正确。有时你将无法打开图像,并且应用程序可能会崩溃。你最好使用这样的代码:

Uri imageUri = data.getData(); 
InputStream imageStream = null; 
try { 
    imageStream = getContentResolver().openInputStream(imageUri); 
    ImageView imageView = (ImageView) findViewById(R.id.imgView); 
    imageView.setImageBitmap(BitmapFactory.decodeStream(imageStream)); 
} catch (FileNotFoundException e) { 
    // Handle the error 
} finally { 
    if (imageStream != null) { 
     try { 
      imageStream.close(); 
     } catch (IOException e) { 
      // Ignore the exception 
     } 
    } 
} 
5

尝试这样

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     super.onActivityResult(requestCode, resultCode, intent); 

     switch (requestCode) { 
       case RESULT_LOAD_IMAGE: 
      if (resultCode == Activity.RESULT_OK) { 

       Uri selectedImage = intent.getData(); 
       try { 
        Bitmap bitmapImage =decodeBitmap(selectedImage); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
           // Show the Selected Image on ImageView 
        ImageView imageView = (ImageView) findViewById(R.id.imgView); 
        imageView.setImageBitmap(bitmapImage); 

      } 

而且

public Bitmap decodeBitmap(Uri selectedImage) throws FileNotFoundException { 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o); 

     final int REQUIRED_SIZE = 100; 

     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) { 
       break; 
      } 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2); 
    } 
+0

您的代码工作正常。但如何增加ImageView的高度和高度? – reegan29

+1

作品般的魅力:) –

-1
final int SELECT_PICTURE = 1; 
public void openGallary(){ 
Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent,"SelectPicture"),SELECT_PICTURE); 
} 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
     case SELECT_PICTURE: 
    Uri selectedImageUri = data.getData(); 
    imgPerview.setImageURI(selectedImageUri);}} 
0

将此图片添加到imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

imageView.setImageURI(selectedImage); 

它适合我。