无法解码流。 (找不到文件异常)

时间:2015-10-30 07:35:52

标签: android image camera decode bitmapfactory

我试图将图像上传到从我的相机中取出的服务器。在这个过程中,当我尝试解码图像时,它会抛出File not found异常。请帮我解决这个问题:

Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
                    .format(new Date());

            File file123 = new File(Environment
                    .getExternalStorageDirectory()
                    + "/sdcard/BusinessVibes/", "IMG_" + timeStamp
                    + ".jpg");

            tempURI = Uri.fromFile(file123);
            Log.i("tempURI", "tempURI: " + tempURI);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);

            startActivityForResult(cameraIntent, CAMERA_REQUEST);
            dialog1.dismiss();


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

        Log.i("camera intent", "camera intent");


        Log.i("temp uri", "temp uri: " + tempURI.toString());
        decodeFile(tempURI.toString());
        UploadImage_textView.setEnabled(true);
        // new ImageUploadTask(encoded).execute();

    }

}


public void decodeFile(String filePath) {
    // Decode image size
    Log.i("decodeFile", "sellDetails_productName: " + filePath);

    Log.i("$$$$$$$$$$$$$$$$$$$", "$$$$$$$$$$$$$$$$$");

    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);  // file not found exception is thrown in this line

    // The new size we want to scale to final
    int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    Log.i("width_tmp", "width_tmp: " + width_tmp);
    Log.i("height_tmp", "height_tmp: " + height_tmp);
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

以上是我参考的代码。当我尝试检查移动设备中的路径时,图像存在。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

你应该试试。

onActivityResult();

 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 imgPath = cursor.getString(columnIndex);
            cursor.close();

答案 1 :(得分:0)

试试这个:

Intent cameraIntent= new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
Uri outputfile=Uri.fromFile(Environment.getExternalStorageDirectory()
        + "/image/"+timeStamp ;);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputfile);
if(intent.resolveActivity(getPackageManager())!=null)
    startActivityForResult(intent, CAMERA_REQUEST_3);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST_1 && resultCode == RESULT_OK ) {  
        Bitmap capturedImage = new BitmapLoader().loadBitmap(defaultPath, 250, 200);
        img1.setImageBitmap(capturedImage);
        File folder = new File(Environment.getExternalStorageDirectory().toString()
                + "/image/"+timeStamp );
                File[] listOfFiles = folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) 
            listOfFiles[i].delete();
        if(Build.VERSION.SDK_INT != Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT != Build.VERSION_CODES.LOLLIPOP) 
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory()+ "/image/")));
        else
        {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
            Uri contentUri = Uri.fromFile(folder); 
            mediaScanIntent.setData(contentUri); 
            sendBroadcast(mediaScanIntent);
        }
    }
}

public static Bitmap loadBitmap(String filePath,int requiredWidth,int requiredHeight){
    BitmapFactory.Options options= getOptions(filePath,requiredWidth, requiredHeight);
    options.inMutable=true;
    Bitmap capturedImage = BitmapFactory.decodeFile(filePath,options);
    Canvas canvas = new Canvas(capturedImage);
    return capturedImage;
}