2011-08-08 61 views
3

我有一個從設備圖庫中檢索圖像並上傳到服務的活動。現在,爲了優化目的,我希望避免上傳Picasa上的圖像,只保存其ID或URL以供以後檢索。如何從圖庫中檢索圖片的Picasa ID/URL

所以我的問題是,我如何檢索該信息。我的意圖代碼粘貼在下面並檢索圖像的URI。

Intent galleryIntent = new Intent(); 
galleryIntent.setType("image/*"); 
galleryIntent.setAction(Intent.ACTION_PICK); 
startActivityForResult(galleryIntent, GALLERY_PIC_REQUEST); 

我試圖查找PICASA_ID(MediaStore.Images.Media.PICASA_ID),但通過使用上述方法,則返回null。有任何想法嗎?

+0

沒人?任何人都可以給我一些關於如何從顯示在用Picasa徽標標記的相冊中的圖片中檢索PICASA_ID的提示嗎?這甚至有可能嗎? – oviroa

+0

按照picasa的意思是:http://picasa.google.com/? –

+0

嗯,不完全。 Android默認圖片庫顯示Picasa中的相冊連接到您的Google帳戶。 – oviroa

回答

0
  • 發動ACTION_GET_CONTENT意圖,而不是ACTION_PICK

  • 提供一個額外的MediaStore.EXTRA_OUTPUTURI到一個臨時文件。


添加到您的調用活動

File yourFile; 

現在使用這個代碼來獲得意圖

yourFile = getFileStreamPath("yourTempFile"); 
yourFile.getParentFile().mkdirs(); 
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT, null); 
galleryIntent .setType("image/*"); 
galleryIntent .putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(yourFile)); 
galleryIntent.putExtra("outputFormat", Bitmap.CompressFormat.PNG.name()); 
startActivityForResult(galleryIntent, GALLERY_PIC_REQUEST); 

請確認yourFile在您調用活動創建

而且

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch(requestCode){ 
    case GALLERY_PIC_REQUEST: 
     File file = null; 
     Uri imageUri = data.getData(); 
     if (imageUri == null || imageUri.toString().length() == 0) { 
      imageUri = Uri.fromFile(mTempFile); 
      file = mTempFile; 
      //this is the file you need! Check it 
     } 
     //if the file did not work we try alternative method 
     if (file == null) { 
      if (requestCode == 101 && data != null) { 
       Uri selectedImageUri = data.getData(); 
       String selectedImagePath = getPath(selectedImageUri); 
       //check this string to extract picasa id 
      } 
     } 
    break; 
    } 
} 

public String getPath(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    if(cursor!=null) 
    { 
     int index = cursor 
     .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(index); 
    } 
    else return null; 
} 
+0

嗯,我已經想出了第一部分,但這不是我想要做的。我不想從本地下載Picasa文件,這是一個非常昂貴的帶寬和資源繁重的操作。我只需要picasa ID,這樣我就可以將其附加到我的數據中。我會嘗試第二塊(如果文件== null),如果這有效,比我有一個答案。謝謝。 – oviroa

+0

我看了看答案,看起來我的問題還沒有被理解。我試圖做的是避免以任何方式從Picasa下載圖片。因此,將其作爲臨時文件本地存儲不會。我詢問是否有任何方法從內置圖庫/相冊中檢索有關該圖像的任何信息(id,picasa url等)。 – oviroa

+0

此外,上述解決方案不起作用,不在我的N1上。圖像不能存儲在卡上,我geterrors和空路徑:08-15 18:35:03。747:錯誤/(11427):不是JPEG:/mnt/sdcard/DCIM/Camera/1313433294232.jpg 08-15 18:35:03.797:錯誤/(11427):不是JPEG:/ mnt/sdcard/DCIM /相機/1313433294232.jpg 08-15 18:35:03.847:錯誤/ PicasaAPI(11427):getAlbums:意外的狀態碼403數據:[email protected]。其次,如果我只是執行文件== null部分,我會得到一個包含長數字的路徑,該數字是臨時UID,但未連接到任何Picasa標識。 – oviroa

-1
@Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     dir =new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/MyImages"); 
     dir.mkdir(); 
     filename = ("Image_" + String.valueOf(System.currentTimeMillis()) + ".poc"); 
    } 

protected Uri getTempFile() 
    { 
     File file = new File(dir,filename); 
     muri = Uri.fromFile(file); 
     return muri; 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     menu.add("Pick Image"); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // TODO Auto-generated method stub 
     super.onOptionsItemSelected(item); 
     openOptionsChooseDialog(); 
     return true; 
    } 

private void openOptionsChooseDialog() 
    { 
      AlertDialog.Builder builder = new AlertDialog.Builder(AppActivity.this).setTitle("Select Image").setItems(items, new DialogInterface.OnClickListener() 
      { 
       public void onClick(DialogInterface dialog, int item) 
       { 
        Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_PICK); 
         intent.setType("image/*"); 
         intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); 
         intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile()); 
         startActivityForResult(intent, SELECT_PICTURE); 
} 

      }); 
      final AlertDialog alert = builder.create(); 
      alert.show(); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     super.onActivityResult(requestCode, resultCode, data); 

     switch(requestCode) 
     { 
case SELECT_PICTURE : if (resultCode == RESULT_OK) 
      { 
        filepath = muri.getPath(); 
        Toast.makeText(this, filepath, Toast.LENGTH_SHORT).show(); 
       //can do bla bla bla... 
      } 

我用同樣的方法,它woks.Hope它可以幫助ü太..

+1

這不會回答我的問題,您正在/ MyFiles下的本地文件下載文件名中的時間戳,但不檢索任何Picasa標識信息。本地文件,我只需要相應文件的picasa url,這樣我就可以存儲它並在以後使用它。這需要在用戶脫機時工作,因此用於測試目的,在飛行模式下。 – oviroa