2013-05-08 4 views
22

मैं MediaStore.Images.Media सामान्य रूप सेक्या मैं एंड्रॉइड में यूरी से छवि फ़ाइल चौड़ाई और ऊंचाई प्राप्त कर सकता हूं?

के माध्यम से छवि चौड़ाई हो रही कर सकते हैं, लेकिन मैं छवि से छवि चौड़ाई और ऊंचाई जो ड्रॉपबॉक्स से चयनित

तो वर्तमान में मैं ड्रॉपबॉक्स से हो रही छवि का आकार करने के लिए विधि निम्नलिखित है हो रही करने की जरूरत है

private void getDropboxIMGSize(Uri uri){ 
    String size = Long.toString(new File(uri.getPath()).length()); 
    return size; 
} 

लेकिन क्या मैं वास्तव में फ़ाइल की चौड़ाई और ऊंचाई से प्राप्त होने वाले की जरूरत

किसी को भी कि कैसे प्राप्त करने के लिए पता है? कृपया मदद!

उत्तर

67
private void getDropboxIMGSize(Uri uri){ 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(new File(uri.getPath()).getAbsolutePath(), options); 
    int imageHeight = options.outHeight; 
    int imageWidth = options.outWidth; 

} 

कोई रास्ता नहीं है। आपको बिटमैप ऑब्जेक्ट बनाना होगा। यदि आप inJustDecodeBounds ध्वज का उपयोग करते हैं तो बिगमैप को मोमरी में लोड नहीं किया जाएगा। वास्तव में BitmapFactory.decodeFile शून्य वापस आ जाएगा। मेरे उदाहरण में uri छवि के लिए phisical मार्ग है

+5

+1 अच्छा जवाब:

यह एक वर्ग SizeFromImage.java आप इसे इस तरह का उपयोग कर सकते हैं। –

+0

इस तरह से ओम एफसी का कारण बन जाएगा? चौड़ाई और ऊंचाई प्राप्त करने के बाद शून्य/रीसाइक्लिंग पर सेट करने की आवश्यकता नहीं है? –

+0

संख्या। चूंकि बिटमैप स्मृति में लोड नहीं होगा क्योंकि यह ओओएम का कारण नहीं बनता है और न ही आपको इसे रीसायकल करने की आवश्यकता है। – Blackbelt

3

समाधान के बजाय new File(uri.getPath()).getAbsolutePath() का उपयोग है uri.toString()

+0

धन्यवाद। आप अपना समय बचाते हैं। – hasanghaforian

+1

यह कोई समाधान नहीं है, क्योंकि यूरी वास्तव में फाइल नहीं हो सकती है। –

4

Blackbelt के जवाब विकल्प का उपयोग कर समय के सबसे अधिक काम करेंगे, लेकिन मैं से एक और समाधान या फ़ॉलबैक समाधान प्रस्तावित चाहते हैं ExifInterface का उपयोग कर। यदि आपके पास छवि यूआरआई है, तो आप पूर्णपैथ का उपयोग कर ExifInterface बना सकते हैं, बिटमैप ऑब्जेक्ट की आवश्यकता नहीं है और न ही बिटमैप फैक्ट्री। विकल्प।

पूर्व।

int width = exif.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, defaultValue); 
int height = exif.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, defaultValue); 
1

इसके अलावा जवाब @Blackbelt करने के लिए आप उरी से फ़ाइल पथ को पुनः प्राप्त करने के लिए निम्न कोड का उपयोग करना चाहिए: यदि आप किसी फ़ाइल uri है

public static String getPathFromURI(Context context, Uri contentUri) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && 
      DocumentsContract.isDocumentUri(context, contentUri)) { 
     return getPathForV19AndUp(context, contentUri); 
    } else { 
     return getPathForPreV19(context, contentUri); 
    } 
} 

private static String getPathForPreV19(Context context, Uri contentUri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null); 
    if (cursor != null && cursor.moveToFirst()) { 
     try { 
      int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      return cursor.getString(columnIndex); 
     } finally { 
      cursor.close(); 
     } 
    } 

    return null; 
} 

@TargetApi(Build.VERSION_CODES.KITKAT) 
private static String getPathForV19AndUp(Context context, Uri contentUri) { 
    String documentId = DocumentsContract.getDocumentId(contentUri); 
    String id = documentId.split(":")[1]; 

    String[] column = { MediaStore.Images.Media.DATA }; 
    String sel = MediaStore.Images.Media._ID + "=?"; 
    Cursor cursor = context.getContentResolver(). 
      query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
        column, sel, new String[]{ id }, null); 

    if (cursor != null) { 
     try { 
      int columnIndex = cursor.getColumnIndex(column[0]); 
      if (cursor.moveToFirst()) { 
       return cursor.getString(columnIndex); 
      } 
     } finally { 
      cursor.close(); 
     } 
    } 

    return null; 
} 
1

Blackbelt के जवाब सही है। हालांकि, यदि आप official camera tutorial में नए फ़ाइल प्रदाताओं का उपयोग कर रहे हैं, तो यह काम नहीं करेगा। यह है कि मामले के लिए काम करता है:

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
BitmapFactory.decodeStream(
     getContext().getContentResolver().openInputStream(mPhotoUri), 
     null, 
     options); 
int imageHeight = options.outHeight; 
int imageWidth = options.outWidth; 
2

यह set of utilities एंड्रॉयड में आकार अमूर्त के साथ काम करने के लिए।

ISize size = new SizeFromImage(imgFilePath); 
size.width(); 
size.hight(); 
संबंधित मुद्दे

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