2013-08-22 39 views
0

創建我先建立用戶可以創建並命名的文件夾,按下按鈕移動到下一個相機的主要的事情。 文件夾被創建後,但圖像無法將它們保存在dall'intent路徑中。 我很困惑完全 我爲我的英語道歉如何發送圖像並保存在文件夾中,從用戶的Android

 final String direct = this.getIntent().getStringExtra("key"); 
public static final int MEDIA_TYPE_IMAGE = 1; 

/** Create a file Uri for saving an image or video */ 

Uri uriSavedImage=Uri.fromFile(new File(direct + "photo.jpg")); 

final Uri getOutputMediaFileUri(int type){ 
     return Uri.fromFile(getOutputMediaFile(type)); 
} 

/** Create a File for saving an image or video */ 
final File getOutputMediaFile(int type){ 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES), "direct"); 
    // This location works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 

    // Create the storage directory if it does not exist 
    if (! mediaStorageDir.exists()){ 
     if (! mediaStorageDir.mkdirs()){ 
      Log.d("MyCameraApp", "failed to create directory"); 
      return null; 
     } 
    } 

    // Create a media file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    File mediaFile; 

    if (type == MEDIA_TYPE_IMAGE){ 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
     "IMG_"+ timeStamp + ".jpg"); 
    } else { 
     return null; 
    } 

    return mediaFile; 
} 

}

主要是這

公共類MainActivity延伸活動{

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 


     EditText text = (EditText)findViewById(R.id.editText1); 
     EditText text2 = (EditText)findViewById(R.id.editText2); 
     EditText text3 = (EditText)findViewById(R.id.editText3); 



     @Override 



     public void onClick(View v) { 



      final String name = text.getText().toString(); 
      final String placeName = text2.getText().toString(); 
      final String dettaglio =text3.getText().toString(); 



      String place = placeName.substring(0,3); 
      String direct = name + place + dettaglio ; 

      File folder = new File("/sdcard/CameraTest/" + direct + "/"); 
      folder.mkdirs(); 



      Intent myIntent = new Intent(MainActivity.this, CameraView.class); 
      myIntent.putExtra("key", "/sdcard/CameraTest/" + direct + "/"); 
      startActivity(myIntent); 



     } 
    }); 

} 

}

回答

0

我認爲它會起作用你:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     Button b = (Button) findViewById(R.id.button1); 
     b.setOnClickListener(new OnClickListener() { 


     EditText text = (EditText) findViewById(R.id.editText1); 
     EditText text2 = (EditText) findViewById(R.id.editText2); 
     EditText text3 = (EditText) findViewById(R.id.editText3); 


     @Override 


     public void onClick(View v) { 


      final String name = text.getText().toString(); 
      final String placeName = text2.getText().toString(); 
      final String dettaglio = text3.getText().toString(); 

      String place = placeName.substring(0, 3); 
      String direct = name + place + dettaglio; 

      Intent myIntent = new Intent(MainActivity.this, CameraView.class); 
      myIntent.putExtra("key", "/CameraTest/" direct + "/"); 
      startActivity(myIntent); 


     } 
    }); 

} 

和你的第二個活動,你會使用你的函數這種方式得到的URI:

final String direct = this.getIntent().getStringExtra("key"); 

Uri uriSavedImage = getOutputMediaFileUri(); 

final Uri getOutputMediaFileUri(){ 
    return Uri.fromFile(getOutputMediaFile()); 
} 

final File getOutputMediaFile(){ 
    // To be safe, you should check that the SDCard is mounted 
    // using Environment.getExternalStorageState() before doing this. 

    File mediaStorageDir = new File(Environment.getExternalStorageDirectory().getPath().toString(), direct); 
    // This location works best if you want the created images to be shared 
    // between applications and persist after your app has been uninstalled. 

    // Create the storage directory if it does not exist 
    if (! mediaStorageDir.exists()){ 
     if (! mediaStorageDir.mkdirs()){ 
      Log.d("MyCameraApp", "failed to create directory"); 
      return null; 
     } 
    } 

    File mediaFile = new File(mediaStorageDir.getPath() + "photo.jpg"); 

    return mediaFile; 
} 

不要忘記權限添加到您的清單文件:

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

我希望我的回答能幫助你。

相關問題