2016-03-30 4 views
0

whit ANDROID previous kitkat問題なくSDカードに画像を保存できました。Android Lollipop画像をSD-CARDにバックグラウンドでダウンロード

私はAndroid Lollipopの新しいアクセスストアフレームワークをどのように使用するのか理解できませんし、私の場合には便利かどうかわかりません。

以下このコードは、私のプロジェクトの一部であり、それは完全に

private Bitmap getBitmap(String url, String image){ 

     String root = Environment.getExternalStorageDirectory().toString(); 
     root = "/mnt/extSdCard/"; 


     Bitmap bitmap = null; 

     File f=fileCache.getFile(url); 

     //from SD cache 
     bitmap = decodeFile(f); 
     if(bitmap!=null) 
      return bitmap; 


     try{ 
      bitmap = BitmapFactory.decodeFile(root + image); 
      if(bitmap!=null){ 
       return bitmap; 
      } 
     }catch (Exception e) { 
      e.printStackTrace(); 
     } 

     //from web 
     if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
      Foto foto = new Foto(); 
      try { 
       try{ 

        foto = new ObjectMapper().readValue(new URL("http://www.xxxxxx.yyyy/project/script.php?id="+url), 
           Foto.class); 

        if(foto!=null){ 
         String[] values = image.split("/"); 
         File dir = new File(root + values[0]); 
         if (!dir.exists()) { 
          dir.mkdir(); 
          String fname = values[1]; 
          File file = new File (dir, fname); 
          bitmap = BitmapFactory.decodeByteArray(foto.getFoto(), 0, foto.getFoto().length); 
          try { 
           FileOutputStream out = new FileOutputStream(file); 
           bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
           out.flush(); 
           out.close(); 

          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
         }else{ 
          String fname = values[1]; 
          File file = new File (dir, fname); 
          bitmap = BitmapFactory.decodeByteArray(foto.getFoto(), 0, foto.getFoto().length); 
          try { 
           FileOutputStream out = new FileOutputStream(file); 
           bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); 
           out.flush(); 
           out.close(); 

          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
         } 

         //bitmap = decodeFile(f); 
         return bitmap; 
        }else{ 
         return bitmap; 
        } 
       }catch(Exception e){ 
        e.printStackTrace(); 
        return null; 
       } 


      } catch (Exception ex){ 
       if (ex instanceof SQLiteConstraintException){ 
        bitmap = BitmapFactory.decodeByteArray(foto.getFoto(), 0, foto.getFoto().length); 
        return bitmap; 
       }else{ 
        ex.printStackTrace(); 
        return null; 
       } 
      } 
     }else{ 
      return bitmap; 
     } 
    } 

私は、Androidの新しいASFを使用するには、この変更をどのように働いています?

Environment.getExternalStorageDirectory().getAbsolutePath();を使用すると、SDカードのパスを取得できません。 SAFを使用

おかげ

+0

現在直面しているエラーは何ですか?ハードコードされた文字列にルート値を再割り当てするのはなぜですか?このEnvironment.getExternalStorageDirectory()。getAbsolutePath();を使用します。もし私が 'Environment.getExternalStorageDirectory()。getAbsolutePath();を使用した場合、 – Stallion

+0

私はsdカードのパスを得ることができません –

答えて

-1

この例では、私はSDカードに書き込むことができた:

public class MainActivity extends AppCompatActivity { 

Uri sdCardUri; 

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

     Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); 
     startActivityForResult(intent, 1); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) 
    { 
     case 1: 

      if (resultCode == Activity.RESULT_OK) { 
       sdCardUri = data.getData(); 

       getContentResolver().takePersistableUriPermission(sdCardUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION); 
      } 
      createFile(); 

      break; 
    } 
} 

private void createFile() { 
    DocumentFile sdCard=DocumentFile.fromTreeUri(this,sdCardUri); 
    DocumentFile createFile=sdCard.findFile("teste.txt"); 
    if (createFile==null) 
     createFile=sdCard.createFile("text","teste.txt"); 
    OutputStream outStream = null; 
    try { 
     outStream = getContentResolver().openOutputStream(createFile.getUri()); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    try { 
     String teste="Isto é um teste"; 
     outStream.write(teste.getBytes()); 
     outStream.flush(); 
     outStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

}

で説明したように、私は、完全なアクセス権を得るためにSDカードを選択するデSAFを使用https://metactrl.com/docs/sdcard-on-lollipop/

+0

これは有効な解決策ではありません...アンドロイドスタジオエミュレータでは'/sdcard/Download'ディレクトリは書き込み可能です –

+0

まだ分かりませんsdcardのユーザマニュアル選択を回避する方法、それに取り組んでいます –

関連する問題