Android BitmapFactory.decodeFile(路径,选项)返回options.outWidth等于0

Android BitmapFactory.decodeFile(路径,选项)返回options.outWidth等于0,android,android-bitmap,bitmapfactory,image-compression,Android,Android Bitmap,Bitmapfactory,Image Compression,下面是我用来压缩图像的代码片段: public static final List<Object> compressImage(String imagePath) { Bitmap scaledBitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;

下面是我用来压缩图像的代码片段:

    public static final List<Object> compressImage(String imagePath) {
        Bitmap scaledBitmap = null;

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;

        Bitmap bmp = BitmapFactory.decodeFile( imagePath, options );

        int actualHeight = options.outWidth;
        int actualWidth  = options.outWidth;

        float maxHeight = 816.0f;
        float maxWidth = 612.0f;
        float imgRatio = actualWidth / actualHeight;
        float maxRatio = maxWidth / maxHeight;

        ................

        return List<Object>
    }
类进程代理:

public static Uri getOutputMediaFileUri( int type, Context context ) {
    return Uri.fromFile( getOutputMediaFile( type, context ) );
}

private static File getOutputMediaFile( int type, Context context ) {
    // Obtem o nome do app para usar como o nome da pasta onde as imagens serao salvas dentro da pasta "Pictures"
    PackageManager packageManager = context.getPackageManager();
    ApplicationInfo applicationInfo = null;

    try {
        applicationInfo = packageManager.getApplicationInfo( context.getApplicationInfo().packageName, 0 );
    } catch ( final PackageManager.NameNotFoundException e ) {
    }
    String nomeApp = (String) (applicationInfo != null ? packageManager.getApplicationLabel( applicationInfo ) : "Desconhecido");

    if (nomeApp == null)
        nomeApp = context.getString(R.string.app_name);

    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    // Esta localizacao trabalha melhor se voce quer criar imagens para ser compartilhada entre aplicacoes e persistir depois de seu app ter sido desinstalado.
    File mediaStorageDir = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), nomeApp );

    // Cria o diretorio se ele nao existe
    if ( !mediaStorageDir.exists() ) {
        if ( !mediaStorageDir.mkdirs() ) {
            Log.d( nomeApp, "Falha ao criar diretório ou diretório já existe!" );
            return null;
        }
    }

    // Cria o nome do arquivo de midia
    String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss" ).format( new Date() );
    File mediaFile;
    if ( type == MEDIA_TYPE_IMAGE ) {
        mediaFile = new File( mediaStorageDir.getPath() + File.separator +
                "IMG_" + timeStamp + ".jpg" );
    } else if ( type == MEDIA_TYPE_VIDEO ) {
        mediaFile = new File( mediaStorageDir.getPath() + File.separator +
                "VID_" + timeStamp + ".mp4" );
    } else {
        return null;
    }

    return mediaFile;
}
方法onActivityResult,其中我调用方法来处理图像:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // If finish Activity on startForActivityResult.
    if (resultCode == RESULT_OK) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            processImageCaptured();
        } else if (requestCode == SELECT_IMAGE_ACTIVITY_REQUEST_CODE) {
            fileImageUri = data.getData();
            new ProcessesImageSelectedTask().execute();
        }
    }
    // If cancel Activity on startForActivityResult.
    else if (resultCode == RESULT_CANCELED) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            // User cancel capture image.
        } else if (requestCode == SELECT_IMAGE_ACTIVITY_REQUEST_CODE) {}
    }
    // If an error occurred in the Activity on startForActivityResult.
    else {
        // Image capture fail, warning user.
        Toast.makeText(this, getString(R.string.fail_activity_take_image), Toast.LENGTH_SHORT).show();
    }
}

private void processImageCaptured() {
    galleryAddPic();
    List<Object> image = ProcessaImagens.compactarImagem(fileImageUri.getPath());

    .................
}

private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    mediaScanIntent.setData(fileImageUri);
    this.sendBroadcast(mediaScanIntent);
}

首先,您需要选择图像

public void onClick(View v) {//does whatever code is in here when the button is clicked
                    try {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
                    } catch (ActivityNotFoundException e){
                        Toast.makeText(Seller_Home_Page.this,"No application available to select image",Toast.LENGTH_SHORT).show();
                    }
                }
public void getStringImage(Bitmap bitmap) {

    if (bitmap != null){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 equals to the quality of the photo
        byte[] imageBytes = baos.toByteArray();

        int PhotoSize = imageBytes.length;

        if(PhotoSize <= 15000) {//Change this value to something more reasonable
            //Setting the Bitmap to ImageView
            ItemPhotoPreview.setImageBitmap(bitmap);

            encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        }else{
            //Setting the Bitmap to ImageView
            ItemPhotoPreview.setColorFilter(Color.TRANSPARENT);

            Toast.makeText(Seller_Home_Page.this,"Photo size is too large",Toast.LENGTH_SHORT).show();
        }
    }
}
然后你需要抓取图像数据

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

    //For photo
    //so the if statement checks if the image came through, double checks if the result is okay and triple checks to see if the data is not null.
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        //File path to the photo that is selected.
        Uri ProductPhotoFilePath = data.getData();


        try {

            //Getting the Bitmap from Gallery
            productBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), ProductPhotoFilePath);

            ImagePath = getPath(ProductPhotoFilePath);

            persistProductImage(productBitmap, ImagePath);

            getStringImage(productBitmap);
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(Seller_Home_Page.this,"Please pick a valid photo",Toast.LENGTH_SHORT).show();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
然后压缩图像

public void onClick(View v) {//does whatever code is in here when the button is clicked
                    try {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
                    } catch (ActivityNotFoundException e){
                        Toast.makeText(Seller_Home_Page.this,"No application available to select image",Toast.LENGTH_SHORT).show();
                    }
                }
public void getStringImage(Bitmap bitmap) {

    if (bitmap != null){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 30, baos);//30 equals to the quality of the photo
        byte[] imageBytes = baos.toByteArray();

        int PhotoSize = imageBytes.length;

        if(PhotoSize <= 15000) {//Change this value to something more reasonable
            //Setting the Bitmap to ImageView
            ItemPhotoPreview.setImageBitmap(bitmap);

            encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        }else{
            //Setting the Bitmap to ImageView
            ItemPhotoPreview.setColorFilter(Color.TRANSPARENT);

            Toast.makeText(Seller_Home_Page.this,"Photo size is too large",Toast.LENGTH_SHORT).show();
        }
    }
}

我认为问题在于您试图使用的文件Uri创建图像。 你确定你的应用程序具有该权限吗?因为没有它,相机应用程序无法存储捕获的图像

此外,这可能是一个很好的起点:

可能imagePath无效。你从哪里获得imagePath?@Commonware请看,我编辑了我的问题。我从CameraApp获取imagePath。我想从CameraApp中选择图像,即相机捕获的图像。我有一个从Gallery应用程序中选择图像的代码。我压缩的代码图像和你们不同。是的,我确信我的应用程序有写外部存储权限。@LucasSantos我刚试过你的代码,效果很好。你能提供一些关于你正在尝试代码的设备的信息吗?一些日志会很方便。还有安卓版本和硬件详细信息Samsung Galaxy S6,安卓棉花糖6.0.1@CodeMonkey@LucasSantos由于它是android M,您可能需要从Application manager检查应用程序权限,因为我使用相同的android版本在S6上进行了测试,并且您的代码在授予存储权限的情况下运行良好,但是当我进入应用程序设置并手动禁用存储设置时抛出了算术异常。