2017-01-23 7 views
1

私は、「画像ビュー」と「ボタン」というuploadImageを含むアプリを持っています。私がアップロード画像をクリックすると、画像を選択して画像ビューで設定できる場所から選択オプションが開きます。問題は、画像ビューで画像を設定する前に、私は画像サイズが200kb以上であるかどうかを確認したい場合、見つかったらトーストメッセージを表示してください。アンドロイドの画像ビューに挿入する前に、画像サイズを確認するにはどうすればよいですか?

コード: -

private void showFileChooser() { 
    // Create intent to Open Image applications like Gallery, Google Photos 
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    startActivityForResult(galleryIntent,PICK_IMAGE_REQUEST); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     // When an Image is picked 
     if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK 
       && null != data) { 
      // Get the Image from data 

      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      // Get the cursor 
      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      // Move to first row 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      imgDecodableString = cursor.getString(columnIndex); 
      cursor.close(); 

      // Set the Image in ImageView after decoding the String 
      m_UploadImage.setImageBitmap(BitmapFactory 
        .decodeFile(imgDecodableString)); 

     } else { 
      Toast.makeText(this, "You haven't picked Image", 
        Toast.LENGTH_LONG).show(); 
     } 
    } catch (Exception e) { 
     Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) 
       .show(); 
    } 
} 
+1

http://stackoverflow.com/questions/29137003/how-to-check-image-size-less-then-100kb-android これがあなたを助けてくれることを願っています。 –

+0

イメージの解像度ではなく、KB単位でサイズを確認する必要がありますか? –

答えて

0

次のコードを使用して画像のサイズを確認することができます。

Bitmap bitImage = BitmapFactory.decodeResource(getResources(), 
      R.drawable.ic_launcher);// For example I took ic_launcher 


    Bitmap bitmap = bitImage; 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
    byte[] imageInByte = stream.toByteArray(); 
    long sizeOfImage = imageInByte.length; //Image size 

あなたのコード:

private void showFileChooser() { 
    // Create intent to Open Image applications like Gallery, Google Photos 
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    startActivityForResult(galleryIntent,PICK_IMAGE_REQUEST); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     // When an Image is picked 
     if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK 
       && null != data) { 
      // Get the Image from data 

      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      // Get the cursor 
      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      // Move to first row 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      imgDecodableString = cursor.getString(columnIndex); 
      cursor.close(); 

       // Set the Image in ImageView after decoding the String 
Bitmap bitImage = BitmapFactory.decodeFile(imgDecodableString); 
Bitmap bitmap = bitImage; 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
     byte[] imageInByte = stream.toByteArray(); 
      long sizeOfImage = imageInByte.length; //Image size 
//Code to check image size greater than 20KB 
    if(sizeofImage/1024 > 200){ 
    Toast.makeText(this, "Image size more than 200KB", Toast.LENGTH_LONG) 

    }else{ 
       m_UploadImage.setImageBitmap(bitImage); 

      } 
       } else { 
        Toast.makeText(this, "You haven't picked Image", 
          Toast.LENGTH_LONG).show(); 
       } 
      } catch (Exception e) { 
       Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) 
         .show(); 
      } 
     } 
+0

"U"の代わりに "You"を適切な英語を使用してください。 –

+0

私のコードを編集してください – Vishal

+0

私は画像サイズで何かをしなかった...変数に保存しました。 – Athul

0

それとも、この方法を試すことができ、

まずはあなたのコードで

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap(); 

:これを使用して

:ImageViewの上添付ビットマップ

private void showFileChooser() { 
     // Create intent to Open Image applications like Gallery, Google Photos 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(galleryIntent,PICK_IMAGE_REQUEST); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     try { 
      // When an Image is picked 
      if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK 
        && null != data) { 
       // Get the Image from data 

       Uri selectedImage = data.getData(); 
       String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

       // Get the cursor 
       Cursor cursor = getContentResolver().query(selectedImage, 
         filePathColumn, null, null, null); 
       // Move to first row 
       cursor.moveToFirst(); 

       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       imgDecodableString = cursor.getString(columnIndex); 
       cursor.close(); 

       // Set the Image in ImageView after decoding the String 
       m_UploadImage.setImageBitmap(BitmapFactory 
         .decodeFile(imgDecodableString)); 


       //Get the Bitmap in your ImageView 

Bitmap bitmap = ((BitmapDrawable)m_UploadImage.getDrawable()).getBitmap(); 

       // Then check the Image size 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
    byte[] imageInByte = stream.toByteArray(); 
    long lengthbmp = imageInByte.length; 

    Toast.makeText(this, "Length of the Image :" + lengthbmp, 
         Toast.LENGTH_LONG).show(); 


      } else { 
       Toast.makeText(this, "You haven't picked Image", 
         Toast.LENGTH_LONG).show(); 
      } 
     } catch (Exception e) { 
      Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) 
        .show(); 
     } 
    } 
0

より良いあなたは、別のクラスを作成しているため、将来的にそのクラスでこのパブリックメソッドを書きますこのクラスを使用することができます。あなたのコードの使用では

public class UtilClassName{ 
public static int getFileSize(Uri imageUri,Activity activity){ 
    int kb_size=0; 
    try { 
     InputStream is=activity.getContentResolver().openInputStream(imageUri); 
     int byte_size=is.available(); 
     int kb_size=byte_size/1024; 
    } 
    catch (Exception e){ 
     // here you can handle exception here 
    } 
    return kb_size; 
} 
} 

このロジック

if(UtilClassName.getFileSize(selectedImage,this)<=200){ 
    Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     // Move to first row 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     imgDecodableString = cursor.getString(columnIndex); 
     cursor.close(); 

     // Set the Image in ImageView after decoding the String 
     m_UploadImage.setImageBitmap(BitmapFactory 
       .decodeFile(imgDecodableString)); 
} 
else{ 
    //show a warning to the user 
} 

は、この方法を試してみて、あなたはどんな問題に直面しているなら、私に知らせてください。私は以前この問題に直面していました。私はこのメソッドをファイルコンプレッサークラス用に作成しました。私はあなたが解決策を得ることを望みます。

関連する問題