2015-03-19 70 views
0

每次在拍攝圖像的文件夾創建部分工作正常,但如果我添加的文件夾的創建和圖像邏輯保存的圖像不會被添加到該文件夾​​將圖像添加到文件夾中的SD卡

photoButton.setOnClickListener(new View.OnClickListener() 
{ 
@Override 
public void onClick(View v) 
{ 
cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 
} 
}); 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
if (requestCode == CAMERA_REQUEST && resultCode != RESULT_CANCELED) 
{ 
folder = new File(Environment.getExternalStorageDirectory() + File.separator+"folder/");       

if(!folder.exists()) 

     { 
      folder.mkdirs(); 
      Log.d("SDcard", "Folder created"); 
     } 
     else 
     { 
      Log.d("SDCard", "Folder already exists"); 
     } 
File file = new File(Environment.getExternalStorageDirectory() +  File.separator +"folder/"); 
     Uri photoPath = Uri.fromFile(file); 
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath); ` 
     } 
     } 

的onclick()直接它顯示錯誤消息:「未能提供結果信息」

*請你幫助*

+0

你做錯了,如果你想給出額外的意圖,然後將它添加到你的點擊監聽器 – 2015-03-19 06:26:21

+0

@ Randyka Yudhistira請你詳細說明一下嗎?我已經在oncreate()方法之前聲明瞭cameraIntent對象...所以它的範圍沒有問題..它顯示沒有錯誤!但圖像沒有添加到文件夾 – Gopi 2015-03-19 06:33:58

+0

@戈皮請檢查我的答案。 – mustafasevgi 2015-03-19 08:03:10

回答

1
public void saveBitmapToFile(Bitmap bmp) { 

     File mAppBaseDir; 
     if (isExternalStorageWritable()) 
      mAppBaseDir = new File(Environment.getExternalStorageDirectory(), "FolderName"); 
     else 
      mAppBaseDir = new File(getApplicationContext().getFilesDir().getParent()).getAbsoluteFile(); 

     if (!mAppBaseDir.exists()) { 
      mAppBaseDir.mkdirs(); 
     } 
     File imageDir = new File(mAppBaseDir, "Profile"); 
     if (!imageDir.exists()) 
      imageDir.mkdirs(); 
     File file = new File(imageDir + "/" + "profile.png"); 
     if (file.exists()) { 
      file.delete(); 
     } 
     try { 
      writeBytesToFile(file, bitmapToByte(bmp)); 

     } catch (IOException e) { 
      // show alert for retry choose photo 
      e.printStackTrace(); 
     } 
    } 



public void writeBytesToFile(File file, byte[] bytes) throws IOException { 
     BufferedOutputStream bos = null; 

     try { 
      FileOutputStream fos = new FileOutputStream(file.getPath()); 
      bos = new BufferedOutputStream(fos); 
      bos.write(bytes); 
     } catch (Exception e) { 
      Log.e("", e.getMessage()); 
     } finally { 
      if (bos != null) { 
       try { 
        bos.flush(); 
        bos.close(); 
       } catch (Exception e) { 
        Log.e("", e.getMessage()); 
       } 
      } 
     } 
    } 

public byte[] bitmapToByte(Bitmap bitmapFinally) { 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmapFinally.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     byte[] byteArray = stream.toByteArray(); 
     return byteArray; 
    } 
+0

感謝您的回覆。但編譯器無法解析Tools類。它是用戶定義的類還是它需要任何導入? – Gopi 2015-03-19 07:02:50

+0

對不起@Gopi,我加了bitmapToByte方法。你可以刪除「工具」。 請重試。 – mustafasevgi 2015-03-19 07:15:05

+0

它的工作原理!非常感謝你... – Gopi 2015-03-19 08:34:08

0

我回答這個問題,根據你的c omment 「請您詳細說明」。 CMIIW:

photoButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     folder = new File(Environment.getExternalStorageDirectory() + File.separator + "folder/"); 

     if (!folder.exists()) 

     { 
      folder.mkdirs(); 
      Log.d("SDcard", "Folder created"); 
     } else { 
      Log.d("SDCard", "Folder already exists"); 
     } 
     File file = new File(Environment.getExternalStorageDirectory() + File.separator + "folder/"); 
     Uri photoPath = Uri.fromFile(file); 
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath); 

     startActivityForResult(cameraIntent, CAMERA_REQUEST); 
    } 
}); 

「伊夫宣佈cameraIntent對象onCreate()方法之前」
這是沒問題的,但onActivityResult會觸發你調用startActivityForResult後,再意圖不會有多餘的你給

+0

我已經提到過這個工作在我的問題中(如果我直接添加文件夾創建並保存圖像邏輯到onclick(),它會顯示錯誤,指出「提交結果信息失敗」)logcat顯示「java.lang.RuntimeException:Failure遞交結果ResultInfo {who = null,request = 1888,result = -1,data = null}「並且應用程序自行關閉 – Gopi 2015-03-19 06:46:15

+0

result -1?相機應用程序打開時按下後退按鈕? – 2015-03-19 07:08:15

+0

No.I只按下TICK按鈕 – Gopi 2015-03-19 08:22:08

相關問題