0

我有我的應用程序在原始文件夾中的照片。我想給用戶選擇將該圖像設置爲壁紙或個人資料圖片。當選擇該選項時,應彈出一個對話框。就像這樣: enter image description here如何顯示對話框以將圖像設置爲android中的壁紙或個人資料圖片?

我試圖顯示與下面的代碼

int resId = R.raw.a_day_without_thinking_mobile; 

     Resources resources = this.getResources(); 
     Uri sendUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(resId) + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId)); 

     Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); 
     intent.setDataAndType(sendUri, "image/jpg"); 
     intent.putExtra("mimeType", "image/jpg"); 
     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

     startActivity(Intent.createChooser(intent, "Set As")); 

但該對話框這個給我看像這樣的對話:

enter image description here

我不想設定圖像直接作爲壁紙,而應該顯示對話框,用戶可以從中選擇他/她是否要將圖像用作壁紙或個人資料圖片。

回答

1

首先,你必須從原始文件夾中下載圖像文件到SD卡上,然後使用SD卡文件,圖像設置壁紙

int resId = R.raw.a_day_without_thinking_mobile; 

String filename = getResources().getResourceEntryName(resId); 

       String destfolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/Motivational Quotes"; 
       createDirIfNotExists(destfolder); 

       String destinationPath = destfolder +"/"+ filename + ".jpg"; 
       File destination = new File(destinationPath); 

       InputStream ins = getResources().openRawResource(
         getResources().getIdentifier(filename, 
           "raw", getPackageName())); 





       try { 
        copy2(ins,destination); 
        //Toast.makeText(this, "Image Downloaded", Toast.LENGTH_SHORT).show(); 

        File externalFile=new File(destinationPath); 
        Uri sendUri2 = Uri.fromFile(externalFile); 
        Log.d("URI:", sendUri2.toString()); 

        Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); 
        intent.setDataAndType(sendUri2, "image/jpg"); 
        intent.putExtra("mimeType", "image/jpg"); 
        startActivityForResult(Intent.createChooser(intent, "Set As"), 200); 

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

public void copy2(InputStream src, File dst) throws IOException { 
     InputStream in = src; 

     OutputStream out = new FileOutputStream(dst); 
     //InputStream in = new FileInputStream(src); 

     // Transfer bytes from in to out 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = in.read(buf)) > 0) { 
      out.write(buf, 0, len); 
     } 
     in.close(); 
     out.close(); 
     addImageGallery(dst); 
    } 

    private void addImageGallery(File file) { 
     ContentValues values = new ContentValues(); 
     values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath()); 
     values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); // setar isso 
     values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()); 
     getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 
    } 

    public static boolean createDirIfNotExists(String path) { 
     boolean ret = true; 

     File file = new File(path); 
     if (!file.exists()) { 
      if (!file.mkdirs()) { 
       Log.e("TravellerLog :: ", "Problem creating Image folder"); 
       ret = false; 
      } 
     } 
     return ret; 
    } 

這裏是你可以使用

0

從你的意圖,取出放多餘的,並使用下列數據並輸入

intent.setDataAndType(sendUri, "image/*"); 
+0

所做的改變如你所說的代碼,但它似乎並不奏效。問題仍然是一樣的。 –

相關問題