2016-07-02 5 views
1

としてビットマップを保存私はQRコードを生成し、その後JPEG画像として保存し、私はこのコードを使用します。私のアンドロイドアプリケーションでJPEG画像

imageView = (ImageView) findViewById(R.id.iv); 
final Bitmap bitmap = getIntent().getParcelableExtra("pic"); 
imageView.setImageBitmap(bitmap); 
save = (Button) findViewById(R.id.save); 
save.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      String path = Environment.getExternalStorageDirectory().toString(); 

      OutputStream fOutputStream = null; 
      File file = new File(path + "/Captures/", "screen.jpg"); 
      if (!file.exists()) { 
       file.mkdirs(); 
      } 

      try { 

       fOutputStream = new FileOutputStream(file); 

       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream); 

       fOutputStream.flush(); 
       fOutputStream.close(); 
       MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
       return; 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return; 
      } 
     } 
    }); 

が、それは常に行で例外をキャッチ:

fOutputStream = new FileOutputStream(file); 

この問題の原因

+0

あなたは何を得ていますか?エラーログを投稿してください – Newbiee

+0

API 23をターゲットに設定していますか?実行時にストレージ権限を要求しましたか? – ianhanniballake

+0

FileNotFound例外@Newbiee –

答えて

2

この問題の原因

file.mkdirs();というステートメントは、screen.jpgという名前のディレクトリを作成しました。 FileOutputStreamは、名前がscreen.jpgのファイルを作成できませんでしたが、その名前のディレクトリが見つかりました。だから、得た:以下のスニペットによって

File file = new File(path + "/Captures/", "screen.jpg"); 
if (!file.exists()) { 
    file.mkdirs(); 
} 

java.io.FileNotFoundException 

次のスニペットを交換してもらえ

String dirPath = path + "/Captures/";  
File dirFile = new File(dirPath); 
if(!dirFile.exists()){ 
    dirFile.mkdirs(); 
} 
File file = new File(dirFile, "screen.jpg"); 

と結果を参照してください?

関連する問題