2011-08-05 111 views
0

我有一個應用程序,我必須從相機活動中捕獲圖像,並使用內容解析程序將該圖像插入到媒體中。 EXTERNAL_CONTENT_URI和我有getString()該圖像的路徑,並通過它捆綁在其他活動。Android:從指定路徑獲取圖像的問題

從那裏,我已經得到該路徑,並把它放在位圖bitmap = BitmapFactory.decodeFile(filepath);

但它顯示FILENOTFOUNDEXCEPTION和NULLPOINTEREXCEPTION.How解決這個問題?

所以我也嘗試另一種方法,使得我在第一個活動中得到的圖像應該先設置爲一個文件,然後我輕鬆解碼該文件?

請建議我這樣做。

更新 - >

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

可否請您上傳一些代碼?這是很難說,沒有代碼... – Kgrover

+0

我已經清除馬問題,請檢查它... – Geetanjali

回答

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); 
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); 

} 
+0

不檢查馬碼 – Geetanjali