2012-06-27 193 views
0
public class MapDemoActivity extends Activity { 

Button capture; 

    ImageView image; 
    int cameracode=100; 
    Bitmap bm; 
    Boolean result; 
    FileOutputStream fos; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     capture=(Button)findViewById(R.id.capture); 
     capture.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       image=(ImageView)findViewById(R.id.image); 
       Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(i, cameracode); 
       image.setDrawingCacheEnabled(true); 
       bm = image.getDrawingCache(); 

       try { 
        fos = new FileOutputStream("sdcard/image.jpg"); 
        result=bm.compress(CompressFormat.JPEG, 75, fos); 

        fos.flush(); 
        fos.close(); 
       } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 

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

        e.printStackTrace(); 
       } 


      } 
     }); 

    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 

        if(requestCode==100) 
        { 

         bm=(Bitmap) data.getExtras().get("data"); 
         image.setImageBitmap(bm); 


        } 

     super.onActivityResult(requestCode, resultCode, data); 
    } 
} 

JPEG圖像在這個PROG我正在拍攝的圖像從相機&顯示在圖像視圖,然後我試圖 其轉換爲JPEG在SD卡存儲... 但是,當我榨捕獲圖像按鈕PROG得到強制關閉.. &空JPEG文件的SD卡創建...我希望存儲JPEG文件到SD卡保存圖像視圖中的SD卡

回答

0

試着改變你的目標目錄是:

try { 
       fos = new FileOutputStream("/mnt/sdcard/MyActivity/image.jpg"); 
       result=bm.compress(CompressFormat.JPEG, 75, fos); 

       fos.flush(); 
       fos.close(); 

確保在寫入目錄之前創建目錄。

  String dir = "/mnt/sdcard/MyActivity/"; 
     directory = new File(dir); 
     if (!directory.exists()) { 
      directory.mkdirs(); 
     } 
1

startActivityForResult是一個異步調用,因此立即執行該調用之後,等待捕獲完成之前,你有正確的一切。不要這樣,你應該將保存代碼移動到onActivityResult

此外,您不應該硬編碼sdcard,而是改爲使用Environment.getExternalStorageDirectory()

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    capture=(Button)findViewById(R.id.capture); 
    capture.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      image=(ImageView)findViewById(R.id.image); 
      Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(i, cameracode); 
     } 
    }); 

} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 

    if(requestCode==100) 
    { 

     bm=(Bitmap) data.getExtras().get("data"); 
     image.setImageBitmap(bm); 

     if(bm == null) { 
      return; //probably user cancelled; 
     } 

     try { 
      fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.jpg")); 
      result=bm.compress(CompressFormat.JPEG, 75, fos); 

      fos.flush(); 
      fos.close(); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 

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

      e.printStackTrace(); 
     } 

    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

最後,你可能要檢查你實際上是試圖挽救它,因爲用戶可能已經取消圖像捕獲之前獲得的圖像。

除此之外,我建議你發佈完整的logcat堆棧跟蹤。也許在那裏還有其他的邪惡。

+0

無可否認,我的答案要徹底得多。這傢伙有你的答案。 –

+0

它的工作原理......但現在通過使用相機PROG得到強制關閉捕獲圖像之後 – user1461473

+0

請在logcat的堆棧跟蹤添加到您的問題 –

0

我認爲執行startActivityForResult後的部分代碼並且位圖還不可用。您應該將其直接保存在onActivityResult上。

Futhermore,有你問的Android給予意圖這個給你保存圖像:

final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this))); 
startActivityForResult(intent, TAKE_PHOTO_CODE); 

getTempFile是返回我我想要的路徑的功能。

PS:你是否要求清單中的許可?你需要

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.CAMERA" />