2014-01-15 6 views
6

私はあなたのギャラリーから写真を選択し、ボタンを持っているアプリを、持っている、それが正常に動作し、画像を選択した後、私のアプリのショーは、活動に戻ってきたと示してい画像ビュー内の画像。画像ビューでギャラリーやショーから写真を選ぶ

私はプレビューが表示されていないいくつかの特定の画像を選択したときにすべてが、時々正常に動作しますがされています。私もonActivityResultで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); 

    } 
}); 

では私のコードは以下の通りです

.. 働いていないその静止画像を圧縮しようとしています(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、オープンストリーム、などの同様の問題に実行ビットマップなど。それは常にバグがあります。

だから私は、ライブラリを検索し、画像-チュー・ライブラリーライブラリーを発見しました。

私はうまくいけば、それは便利です

などPicasaからの画像のように、あなたは非常に使いやすいですし、あなたのためにすべてのそれらの核心ザラザラの問題を解決しimage-chooser-library

からこのプロジェクトを試してみたい賭け君は。

+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のの高さを高くするwell.Butのですか?魅力:) – 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); 

私のために働きます。

関連する問題