2013-10-01 98 views
-1

正試圖在SD卡上創建一個文件夾並將所有圖像保存在那裏,而不是在內部存儲器上。將圖像保存到SD卡文件夾

public class CameraActivity extends Activity { 

    private static final int CAMERA_PIC_REQUEST = 1111; 
    private ImageView mImage; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mImage = (ImageView) findViewById(R.id.result); 
     //1 
     Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     File image = new File(Environment.getExternalStorageDirectory()+File.separator +"mypics"); 
    Uri fileUri = Uri.fromFile(image); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 

     startActivityForResult(intent, CAMERA_PIC_REQUEST); 
    } 


    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == CAMERA_PIC_REQUEST) { 
      //2 
      Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
      EditText etext = (EditText) findViewById(R.id.editTextLocation); 
      Toast.makeText(getBaseContext(), data.getExtras().get("data").toString(), Toast.LENGTH_LONG).show(); 
      mImage.setImageBitmap(thumbnail); 
      etext.setText(data.getExtras().get("data").toString()); 
      //3 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
      //4 
      String state=Environment.getExternalStorageState(); 
      Toast.makeText(getBaseContext(), state, Toast.LENGTH_LONG).show(); 
      File file = new File(Environment.getExternalStorageDirectory()+File.separator +"mypics"); 
      if(!file.exists()) 
       if(file.mkdirs()) 
        Log.v("Mobitracker","success"); 

      if (Environment.MEDIA_MOUNTED.equals(state)) 
       Toast.makeText(getBaseContext(), "Yes its read only", Toast.LENGTH_LONG).show(); 
      if (Environment.isExternalStorageRemovable()) 
       Toast.makeText(getBaseContext(), "Yes its internal card", Toast.LENGTH_LONG).show(); 

      //Create New file and name it Image2.PNG 
      File file1 = new File(file, "Image2.PNG"); 
      try { 
       file1.createNewFile(); 
       FileOutputStream fo = new FileOutputStream(file1); 
       //5 
       fo.write(bytes.toByteArray()); 
       fo.flush(); 
       fo.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     else 
      Toast.makeText(getBaseContext(), "Please take snap again", Toast.LENGTH_LONG).show(); 
    } 
} 

這是我的代碼,我嘗試了很多東西,但仍然不可能保存在internalmemory DCIM文件夾中爲什麼?

作爲

我得到的輸出裝 是它的只讀

我不連接到系統但還是同樣的問題,我甚至嘗試重新啓動手機和所有,但仍然相同。 我使用的是索尼xperia tipo mobile 4.0.4 os。

請幫我沮喪我試過所有的線程。

你們能幫助我,請

我的權限是

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" /> 
<uses-feature android:name="android.hardware.camera.autofocus" /> 
<uses-permission android:name="android.permission.RECORD_AUDIO" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

而且我也能看到位圖圖像圖像視圖,但 Toast.makeText(getBaseContext(),data.getExtras()。獲得( 「數據」)的toString(),Toast.LENGTH_LONG).show()。

正在恢復一些[email protected]一些事情

+0

您是否在清單文件中指定了相關權限? – GrIsHu

+0

http://android-spirit.blogspot.in/2013/07/camera-intent-in-android.html – Nirmal

+0

是的,我有指定的權限 –

回答

0

您應該使用的OutputStream寫的內容。請致電here尋求幫助。

或者做類似的信息(見here完整牙):

Button.OnClickListener buttonSaveOnClickListener 
= new Button.OnClickListener(){ 

    @Override 
    public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    OutputStream outStream = null; 
    File file = new File(extStorageDirectory, "er.PNG"); 
    try { 
    outStream = new FileOutputStream(file); 
    bm.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
    outStream.flush(); 
    outStream.close(); 

    Toast.makeText(AndroidWebImage.this, "Saved", Toast.LENGTH_LONG).show(); 

    } catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    Toast.makeText(AndroidWebImage.this, e.toString(), Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    Toast.makeText(AndroidWebImage.this, e.toString(), Toast.LENGTH_LONG).show(); 
    } 

    } 

}; 
+0

我總是得到FileNotFound異常..當我試着這個代碼 –

+0

你給了正確的路徑到您的文件? – crazyPixel

0

this的Android培訓仔細張貼理出頭緒。正如帖子裏說,

The Android Camera application saves a full-size photo if you give it a file to save into. 

所以如果你沒有通過文件的Uri,將只存儲在DCIM,你應該使用這個Uri訪問圖像。

Check out similar thread

+0

我可以使用該Uri存儲在數據庫中嗎?我的意思是我可以在其他時間使用相同的uri並檢索圖像嗎? –

+0

是的,你可以使用Uri來存儲數據庫,並檢索圖像。 – GrIsHu

+0

File image = new File(Environment.getExternalStorageDirectory()+ File.separator +「mypics」); Uri fileUri = Uri.fromFile(image); intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);我可以這樣做,並存儲在數據庫中的這個fileUri?什麼是文件名? –