2013-12-11 6 views
9

एसडी कार्ड से केवल एक विशेष छवि लेने का प्रयास करें। मैं गैलरी और फोटो ऐप से किकैट में तस्वीरें लेने में सक्षम हूं। लेकिन नहीं मिल रहा फ़ाइल पथ जब मैं रिकेंट से छवि चुनता हूं तो फ़ाइल पथ को शून्य के रूप में प्राप्त कर रहा हूं।[एंड्रॉइड - किटकैट] एंड्रॉइड के अंतर्निहित गैलरी ऐप से प्रोग्राम प्राप्त करें

मैंने https://stackoverflow.com/a/2636538/1140321 को आजमाया।

+0

तुम सच में ImagePath की ज़रूरत है? क्या आप छवि अपलोड कर रहे हैं? या सिर्फ इसे छविदृश्य में दिखाना चाहते हैं? –

+0

मैं इसे सिर्फ एक छवि दृश्य में दिखाना चाहता हूं .. – Meher

+0

क्या आप अपना कोड पोस्ट कर सकते हैं क्योंकि मेरे लिए –

उत्तर

33

यह Kitkat

public class BrowsePictureActivity extends Activity{ 

private static final int SELECT_PICTURE = 1; 

private String selectedImagePath; 
private ImageView imageView; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.browsepicture); 
    imageView = (ImageView)findViewById(R.id.imageView1); 

    ((Button) findViewById(R.id.button1)) 
      .setOnClickListener(new OnClickListener() { 

       public void onClick(View arg0) { 
        Intent intent = new Intent(); 
        intent.setType("image/*"); 
        intent.setAction(Intent.ACTION_GET_CONTENT); 
        startActivityForResult(Intent.createChooser(intent, 
          "Select Picture"), SELECT_PICTURE); 
       } 
      }); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == SELECT_PICTURE) { 
      Uri selectedImageUri = data.getData(); 
      if (Build.VERSION.SDK_INT < 19) { 
       selectedImagePath = getPath(selectedImageUri); 
       Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath); 
       imageView.setImageBitmap(bitmap); 

      } 
      else { 
       ParcelFileDescriptor parcelFileDescriptor; 
       try { 
        parcelFileDescriptor = getContentResolver().openFileDescriptor(selectedImageUri, "r"); 
        FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor(); 
        Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor); 
        parcelFileDescriptor.close(); 
        imageView.setImageBitmap(image); 

       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

/** 
* helper to retrieve the path of an image URI 
*/ 
public String getPath(Uri uri) { 
     if(uri == null) { 
      return null; 
     } 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 
     if(cursor != null){ 
      int column_index = cursor 
      .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } 
     return uri.getPath(); 
} 

} 

आप अनुमति जोड़नी होगी के लिए काम करता

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

+0

कोड समान नहीं है ... अगर अन्य स्थिति की जांच करें जो किटकैट के लिए जांचती है: पी –

+0

क्या आपने कोड को आजमाया है? क्या यह आपके लिए काम कर रहा है? –

+0

@ सुनील मिश्रा किट-कट में इस से छवि पथ कैसे प्राप्त करें ?? –

संबंधित मुद्दे