2016-07-25 33 views
1

如何使用下載管理器如何使用下載管理器在Android的

代碼保存在內部存儲的圖像或MP3文件存儲下載的圖像在內部存儲文件在我的內部存儲 但它不能很好地工作 所需的路徑是「數據/數據/包/ app_channel1/image1.jpg」

+0

什麼是您的logcat都這麼說? –

+0

圖像下載perfactly和它保存在SD卡,我可以很容易地打開該圖像,但我想保存該圖像在我的應用程序包和下載的圖像只能這個應用程序 – Chetna

+0

File directory = cw.getDir(「channel」+ cId,Context .MODE_PRIVATE);這就是爲什麼只有你的應用程序可以打開圖像 –

回答

5

只有你自己的應用程序可以Access到App internal storage默認android內置下載管理器無法訪問您的應用程序內部存儲器,因此您不能在內部存儲器中下載。

解決方案:

下載文件中sd card臨時文件,當下載完成register a receive,然後從external複製文件到internal storage.

完整代碼:

public class MainActivity extends Activity { 
    private long enqueue; 
    private DownloadManager dm; 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     BroadcastReceiver receiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       String action = intent.getAction(); 
       if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) { 
        long downloadId = intent.getLongExtra(
          DownloadManager.EXTRA_DOWNLOAD_ID, 0); 
        DownloadManager.Query query = new DownloadManager.Query(); 
        query.setFilterById(enqueue); 
        Cursor c = dm.query(query); 
        if (c.moveToFirst()) { 
         int columnIndex = c 
           .getColumnIndex(DownloadManager.COLUMN_STATUS); 
         if (DownloadManager.STATUS_SUCCESSFUL == c 
           .getInt(columnIndex)) { 

          ImageView view = (ImageView) findViewById(R.id.imageView1); 
          String uriString = c 
            .getString(c 
              .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); 

          Uri a = Uri.parse(uriString); 
          File d = new File(a.getPath()); 
          // copy file from external to internal will esaily avalible on net use google. 
          view.setImageURI(a); 
         } 
        } 
       } 
      } 
     }; 

     registerReceiver(receiver, new IntentFilter(
       DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 
    } 

    public void onClick(View view) { 
     dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
     DownloadManager.Request request = new DownloadManager.Request(
       Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png")).setDestinationInExternalPublicDir("/Sohail_Temp", "test.jpg"); 
     enqueue = dm.enqueue(request); 
    } 

    public void showDownload(View view) { 
     Intent i = new Intent(); 
     i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS); 
     startActivity(i); 
    } 
} 

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="onClick" 
     android:text="Start Download"></Button> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="showDownload" 
     android:text="View Downloads"></Button> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/image_1"></ImageView> 
</LinearLayout> 

權限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

你可以給它示例代碼 – Chetna

+0

@Chetna檢查更新的答案,如果有幫助請打勾並投票。 –

+0

使用此代碼圖像存儲在SD卡中,我想要保存圖像在自己的應用程序包' – Chetna