Android:从指定路径获取图像时出现问题

时间:2011-08-05 06:09:46

标签: android android-2.2-froyo

我有一个应用程序,我必须从相机活动中捕获图像,并使用内容解析器,我已将该图像插入媒体。 EXTERNAL_CONTENT_URI ,我有getString()该图像的路径和在其他活动中将其传递给捆绑。

从那里开始,我已经得到了这个路径并把它放在Bitmap中bitmap = BitmapFactory.decodeFile(filepath);

但它显示了FILENOTFOUNDEXCEPTION和NULLPOINTEREXCEPTION。如何解决?

所以我也在尝试另一种方法,这样我在第一个活动中得到的图像应首先设置为文件,然后我可以轻松解码该文件?

请建议我这样做的方法。

更新 - >

CODE: `//pass image path to other activity`

case PICK_FROM_CAMERA : if (resultCode == RESULT_OK)
            { 
                ContentValues values = new ContentValues();
                values.put(Images.Media.TITLE, "title");
                values.put(Images.Media.BUCKET_ID, "test");
                values.put(Images.Media.DESCRIPTION, "test Image taken");
                values.put(Images.Media.MIME_TYPE,"image/jpeg");       
                Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
                filepath = uri.getPath();
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                //((ImageView)findViewById(R.id.selectedimage)).setImageBitmap(photo);
                OutputStream outstream;
                try 
                {
                    outstream = getContentResolver().openOutputStream(uri);
                    photo.compress(Bitmap.CompressFormat.JPEG,100,outstream);
                    outstream.close();
                }
                catch (FileNotFoundException e) {}
                catch (IOException e) {}
                Intent intent = new Intent(this.getApplicationContext(),AnimationActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("flag", 0);
                bundle.putString("filepath", filepath);
                intent.putExtras(bundle);
                startActivity(intent);
            }


Code: //show that image 

public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView img = (ImageView)findViewById(R.id.background);    
        Bitmap border = BitmapFactory.decodeResource(getResources(),R.drawable.border1);
        Bundle extra = getIntent().getExtras();
        mfilepath = extra.getString("filepath");
        int flag = extra.getInt("flag");
        if(flag==1)
        {
            bgr= BitmapFactory.decodeFile(mfilepath);
        }
        if(flag==0)
        {
            bgr=BitmapFactory.decodeFile(mfilepath);

            OutputStream outstream;
            try 
            {
                outstream = getContentResolver().openOutputStream(Uri.fromFile(new File(mfilepath)));
                //bgr.compress(Bitmap.CompressFormat.JPEG,100,outstream);
                outstream.close();
            }
            catch (FileNotFoundException e) {}
            catch (IOException e) {}
        }
        bmOverlay = Bitmap.createBitmap(border.getWidth(),border.getHeight(),bgr.getConfig());
        canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(bmOverlay, 0, 0, null);
        canvas.drawBitmap(bgr, 0, 0, null); 
        canvas.drawBitmap(border,0,0, null);
        canvas.save(); 
        img.setImageBitmap(bmOverlay);

2 个答案:

答案 0 :(得分:1)

尝试这种简单的方法,我认为它应该可以解决您的问题。

private void savePicture(String filename, Bitmap b, Context ctx) {
    try {

        FileOutputStream out;
        out = ctx.openFileOutput(filename, Context.MODE_WORLD_READABLE);
        b.compress(Bitmap.CompressFormat.JPEG, 40, out);
        if (b.compress(Bitmap.CompressFormat.JPEG, 40, out) == true)

            out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

编辑1 :虽然我不确定您的要求,但我认为您可能正在寻找此方法:

private void takePicture() {
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File photo = new File(Environment.getExternalStorageDirectory(),
            "Pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    imageUri = Uri.fromFile(photo);
    startActivityForResult(intent, 0);

}

答案 1 :(得分:0)

使用FileOutputStream代替OutputStream解决了问题。

case PICK_FROM_CAMERA : if (resultCode == RESULT_OK)
            { 
                ContentValues values = new ContentValues();
                values.put(Images.Media.TITLE, "title");
                values.put(Images.Media.BUCKET_ID, "test");
                values.put(Images.Media.DESCRIPTION, "test Image taken");
                values.put(Images.Media.MIME_TYPE,"image/jpeg");       
                Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
                filepath = uri.getPath();
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                try 
                {
                    FileOutputStream outstream; =new FileOutputStream(filepath);
                    photo.compress(Bitmap.CompressFormat.JPEG,100,outstream);
                    outstream.close();
                }
                catch (FileNotFoundException e) {}
                catch (IOException e) {}
                Intent intent = new Intent(this.getApplicationContext(),AnimationActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("flag", 0);
                bundle.putString("filepath", filepath);
                intent.putExtras(bundle);
                startActivity(intent);
            }


Code: //show that image 

public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ImageView img = (ImageView)findViewById(R.id.background);    
        Bitmap border = BitmapFactory.decodeResource(getResources(),R.drawable.border1);
        Bundle extra = getIntent().getExtras();
        mfilepath = extra.getString("filepath");
        bgr= BitmapFactory.decodeFile(mfilepath);
        bmOverlay = Bitmap.createBitmap(border.getWidth(),border.getHeight(),bgr.getConfig());
        canvas = new Canvas(bmOverlay);
        canvas.drawBitmap(bmOverlay, 0, 0, null);
        canvas.drawBitmap(bgr, 0, 0, null); 
        canvas.drawBitmap(border,0,0, null);
        canvas.save(); 
        img.setImageBitmap(bmOverlay);