java - 我如何限制所选图像的大小

标签 java android android-intent

我的问题是消耗内存,我需要限制所选图像的大小。

编辑

我不需要在加载后调整图像大小,我可以使用

Bitmap.createScaledBitmap(image, (int) 80, (int) 80, true);

我需要阻止用户选择图片 > 5 MB

我的代码

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
      if (requestCode == SELECT_PICTURE) {
        Uri selectedImageUri = data.getData();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

      }
    }
}

限制文件夹大小不是更好的选择。

解决方案

我用这个方法解决了我的问题:

public boolean MaxSizeImage(String imagePath) {
    boolean temp = false;
    File file = new File(imagePath);
    long length = file.length();

    if (length < 1500000) // 1.5 mb
        temp = true;

    return temp;
}

如果需要imagePath,可以使用这个方法

 public String getImagePath(Uri uri){
     Cursor cursor = getContentResolver().query(uri, null, null, null, null);
     cursor.moveToFirst();
     String document_id = cursor.getString(0);
     document_id = document_id.substring(document_id.lastIndexOf(":")+1);
     cursor.close();

     cursor = getContentResolver().query(
             android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
             null, MediaStore.Images.Media._ID + " = ? ", new String[]{document_id}, null);
     cursor.moveToFirst();
     String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
     cursor.close();

     return path;
 }

最佳答案

试试这个 Hackro :D

首先:

public static final int PICK_IMAGE = 1;
private void takePictureFromGalleryOrAnyOtherFolder() 
{
 Intent intent = new Intent();
 intent.setType("image/*");
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent, "Select Picture"),PICK_IMAGE);
}

然后:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
           if (requestCode == PICK_IMAGE) {
                Uri selectedImageUri = data.getData();
                String imagePath = getRealPathFromURI(selectedImageUri);
               //Now you have imagePath do whatever you want to do now
             }//end of inner if
         }//end of outer if
  }

public String getRealPathFromURI(String contentURI) {
    Uri contentUri = Uri.parse(contentURI);

    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = null;
    try {
        if (Build.VERSION.SDK_INT > 19) {
            // Will return "image:x*"
            String wholeID = DocumentsContract.getDocumentId(contentUri);
            // Split at colon, use second item in the array
            String id = wholeID.split(":")[1];
            // where id is equal to
            String sel = MediaStore.Images.Media._ID + "=?";

            cursor = context.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    projection, sel, new String[] { id }, null);
        } else {
            cursor = context.getContentResolver().query(contentUri,
                    projection, null, null, null);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    String path = null;
    try {
        int column_index = cursor
                .getColumnIndex(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        path = cursor.getString(column_index).toString();
        cursor.close();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return path;
}

关于java - 我如何限制所选图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34445254/

相关文章:

java - 与线程的可变干扰

java - SAXParseException XML-20221 文本中的无效字符

java - 如何给像素着色?

android - 导航后,iframe中的YouTube视频继续播放-Android和Phonegap

android - Google Play 控制台显示自 5 月 7 日起所有应用中的数据不正确

android - 获取 Android 中 createChooser 方法的 IntentSender 对象

android - 图像是从画廊上传的,而不是通过 webView 从相机上传的

Java EBNF?

android - 通过 "onItemClick"在ListView Android item上从SQLite数据库获取数据到intent

android - 如何使 SearchView 始终在 android 中展开?