2016-09-22 159 views
1

我想從圖庫中獲取圖像。它給了我像位圖一樣的圖像。我希望.jpg文件中的圖像能夠保存我的數據庫中的文件名。如何以.jpg文件格式從圖庫中獲取圖像?

我按照這個教程:

http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample

畫廊圖像中選擇代碼:

@SuppressWarnings("deprecation") 
private void onSelectFromGalleryResult(Intent data) { 

    Bitmap bm=null; 
    if (data != null) { 
     try { 
      bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    Uri selectedImage = data.getData(); 

    String[] filePath = {MediaStore.Images.Media.DATA}; 

    Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null); 


    c.moveToFirst(); 

    int columnIndex = c.getColumnIndex(filePath[0]); 

    String picturePath = c.getString(columnIndex); 

    c.close(); 
    File file = new File(picturePath);// error line 

    mProfileImage = file; 

    profile_image.setImageBitmap(bm); 
} 

我嘗試這樣做。但我得到文件上的空指針。

例外:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference 

此外,我不希望這個新創建的文件被保存在外部存儲。這應該是一個臨時文件。我怎樣才能做到這一點?

謝謝..

+0

請檢查http://stackoverflow.com/questions/5383797/open-an-image-using-uri-in-androids-default-gallery-image-viwer – Shailesh

回答

1

好消息是你比你想象的要接近完成!

Bitmap bm=null; 
if (data != null) { 
    try { 
     bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

在這一點上,如果bm != null,你有一個Bitmap對象。位圖是Android的通用圖像對象,已準備就緒。它實際上可能已經是.jpg格式,所以你只需要將它寫入文件即可。你想將其寫入到一個臨時文件,所以我會做這樣的事情:

File outputDir = context.getCacheDir(); // Activity context 
File outputFile = File.createTempFile("prefix", "extension", outputDir); // follow the API for createTempFile 

無論如何,在這一點上它很容易爲Bitmap寫入文件。

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream); //replace 100 with desired quality percentage. 
byte[] byteArray = stream.toByteArray(); 

現在你有一個字節數組。我會留下來寫給你一個文件。

如果你想臨時文件消失,在這裏看到更多的信息:https://developer.android.com/reference/java/io/File.html#deleteOnExit()

Bitmap bm=null; 
if (data != null) { 
    try { 
     bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
if (bm != null) { // sanity check 
    File outputDir = context.getCacheDir(); // Activity context 
    File outputFile = File.createTempFile("image", "jpg", outputDir); // follow the API for createTempFile 

    FileOutputStream stream = new FileOutputStream (outputFile, false); // Add false here so we don't append an image to another image. That would be weird. 
    // This line actually writes a bitmap to the stream. If you use a ByteArrayOutputStream, you end up with a byte array. If you use a FileOutputStream, you end up with a file. 
    bm.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
    stream.close(); // cleanup 
} 

我希望幫助!

+0

我試過你的答案,創建一個臨時文件..但進一步,我轉換文件時出現錯誤。你能檢查編輯的問題嗎? ? @Eric – Sid

+0

@Sid抱歉,我想你誤解了我。一旦你得到了位圖,你想將它轉換爲一個字節數組:'ByteArrayOutputStream stream = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat。JPEG,100,流); //用期望的質量百分比替換100。 byte [] byteArray = stream.toByteArray();'一旦你轉換成一個字節數組,你就可以將該數組寫入文件。在你的代碼中,它看起來並不像你正在做的那樣。 – Eric

0

看起來像你的picturePath爲空。這就是爲什麼你不能轉換圖像。嘗試添加該代碼片段來獲得所選擇的圖像的路徑:

private String getRealPathFromURI(Uri uri) { 
    String[] projection = { MediaStore.Images.Media.DATA }; 
    @SuppressWarnings("deprecation") 
    Cursor cursor = managedQuery(uri, projection, null, null, null); 
    int column_index = cursor 
     .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
} 

之後,你需要修改onSelectFromGalleryResult。刪除/禁用行String[] filePath = {MediaStore.Images.Media.DATA};等,並替換爲以下。

Uri selectedImageUri = Uri.parse(selectedImage); 
String photoPath = getRealPathFromURI(selectedImageUri); 
mProfileImage = new File(photoPath); 

//check if you get something like this - file:///mnt/sdcard/yourselectedimage.png 
Log.i("FilePath", mProfileImage.getAbsolutePath) 
if(mProfileImage.isExist()){ 
    //Check if the file is exist. 
    //Do something here (display the image using imageView/ convert the image into string) 
} 

問題:您需要將它轉換爲.jpg格式的原因是什麼?它可以是.gif,.png等嗎?

相關問題