2013-04-08 47 views
1

我正在構建一個包含大量圖像的應用程序,以用作任何聊天客戶端的替代圖標圖像共享選項。如何從我的應用程序將可繪製資源的ID返回給其他程序包/開發人員的調用應用程序

編輯:我想出瞭如何從另一個應用程序(不是我的)之一啓動我的應用程序,所以現在我可以選擇默認圖庫或我的應用程序選擇圖像。

剩下的是,我需要知道圖庫返回到應用程序的圖像拾取的確切位置,所以我可以從我的應用程序返回相同的東西。

編輯3:我現在通過一個Intent回聊天客戶端作爲這樣

Uri imageURI; 
    Intent shareIntent = new Intent(); 
    switch(v.getId()){ 
    case R.id.eric1 : 
     imageURI = Uri.parse(MY_RESOURCE_URI); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, imageURI); 
     shareIntent.setType("image/plain"); 
     setResult(RESULT_OK, shareIntent); 
     Utils.makeToast("Selected",this); 
     finish(); 

現在即時得到共享文件失敗的錯誤。是否因爲我試圖將資源URI從我的包傳遞給另一個包?

編輯 - 最後解決它

Bitmap image = BitmapFactory.decodeResource(getResources(), resID); 
     File imageFile; 
     Date d = new Date(); 
     int imgName = (Long.toString(d.getTime())).hashCode(); 
     String state = Environment.getExternalStorageState(); 
     printDebug(state); 
     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      File file = getExternalCacheDir(); 
      if (file != null) { 
       try { 
        //String root = file.getAbsolutePath(); 
        imageFile = new File(file, imgName+".png"); 
        printDebug(imageFile.getAbsolutePath()); 
        FileOutputStream stream = new FileOutputStream(imageFile); 
        boolean complete = image.compress(Bitmap.CompressFormat.PNG, 90, stream); 
        if (!complete) { 
         Log.d("tag", "image not saved"); 
        } 
        Log.d("tag", "image saved"); 
        return Uri.parse(imageFile.getAbsolutePath()); 
       } catch (IOException e) { 
        Log.d("tag", "Can't save image", e); 
       } 
      } 
     } 
     return null; 
     } 

感謝您的幫助這樣

+1

當任何應用程序調用你的活動與startActivityForResult()..你可以返回字符串「file/path/to/the/image/in/sdcard」 – MKJParekh 2013-04-08 13:16:09

+0

是的,返回文件的路徑 – 2013-04-08 13:24:44

+0

@MKJParekh:我不知道究竟是什麼他的應用程序我正在返回預計在其onActivityResult方法。我正在寫一個類似的笑臉應用程序的聊天應用程序https://play.google.com/store/apps/details?id=com.androidsx.smileys&hl=en 我已經完成了一切,除了圖像返回部分,這是非常令人沮喪的,因爲即使經過全面淘汰我似乎無法弄清楚什麼是在結果中傳回。 – Hanut 2013-04-08 15:42:24

回答

0

對不起,我忘了將它張貼作爲一個答案,但在這裏它是 -

Bitmap image = BitmapFactory.decodeResource(getResources(), resID); 
    File imageFile; 
    Date d = new Date(); 
    int imgName = (Long.toString(d.getTime())).hashCode(); 
    String state = Environment.getExternalStorageState(); 
    printDebug(state); 
    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     File file = getExternalCacheDir(); 
     if (file != null) { 
      try { 
       //String root = file.getAbsolutePath(); 
       imageFile = new File(file, imgName+".png"); 
       printDebug(imageFile.getAbsolutePath()); 
       FileOutputStream stream = new FileOutputStream(imageFile); 
       boolean complete = image.compress(Bitmap.CompressFormat.PNG, 90, stream); 
       if (!complete) { 
        Log.d("tag", "image not saved"); 
       } 
       Log.d("tag", "image saved"); 
       return Uri.parse(imageFile.getAbsolutePath()); 
      } catch (IOException e) { 
       Log.d("tag", "Can't save image", e); 
      } 
     } 
    } 
    return null; 
    } 
相關問題