2012-01-09 13 views
7

मैं अंतर्निहित एंड्रॉइड गैलरी कॉलिंग ACTION_PICK इरादे से फ़ोटो पुनर्प्राप्त करना चाहता हूं। मुझे Picasa की छवियों में कोई समस्या है। मैंने इस link पर कोड का उपयोग किया है, लेकिन यह काम नहीं करता है (फ़ाइल ऑब्जेक्ट मौजूद नहीं है)। कोई विचार, कृपया।एंड्रॉइड मैं अंतर्निहित गैलरी से पिकासा फोटो कैसे प्राप्त करूं?

उत्तर

0

कोड काम करता है, तो है इस निर्देश का सम्मिलित करें:

intent.putExtra("crop", "true"); 
0
  • लॉन्च के बजाय एक ACTION_GET_CONTENT आशय एक ACTION _PICK
  • एक अस्थायी फ़ाइल के लिए एक यूआरआई के साथ एक MediaStore.EXTRA_OUTPUT अतिरिक्त प्रदान करें।

अपने बुला गतिविधि को यह करें:

फ़ाइल Yourfile;

अभी सेवा का उपयोग इस code to get Intent:

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

mTempFile फ़ाइल का प्रयोग कभी खाली है। –

+1

यह तस्वीर काम नहीं करती है अगर तस्वीर पिकासा में है .. –

5

ACTIVITYRESULT_CHOOSEPICTURE पूर्णांक आप जब बुला का उपयोग करें startActivity (इरादे टी, अनुरोध कोड);

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(requestCode == ACTIVITYRESULT_CHOOSEPICTURE) { 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    final InputStream is = context.getContentResolver().openInputStream(intent.getData()); 
    final Bitmap bitmap = BitmapFactory.decodeStream(is, null, options); 
    is.close(); 
    } 
} 
+0

इसके लिए धन्यवाद, यह ठीक काम करता है, भले ही चित्र पिकासा से आता है। थ्रेड पर यह कोड करना याद रखें, क्योंकि यह नेट से सामग्री डाउनलोड कर सकता है। –

0

इस कोड

final Uri tempUri = data.getData(); 
        Uri imageUri = null; 
        final InputStream imageStream; 
        try { 
         imageStream = getActivity().getContentResolver().openInputStream(tempUri); 
         Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); 
         imageUri = getImageUri(getActivity(), selectedImage); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } 


public Uri getImageUri(Context inContext, Bitmap inImage) { 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
     String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); 
     return Uri.parse(path); 
    } 
संबंधित मुद्दे