2015-09-02 70 views
0

我的代碼在較舊的Android上運行良好,但在Android 4.4上出現錯誤。 我試圖將文件複製到外部存儲,但這沒有幫助。 您可以在日誌中看到該文件複製正常,但由於Media.insertImage上的代碼扼流圈可能無法正確創建Bitmap如何以編程方式將圖像存儲在Android上的圖庫4.4

請注意,如果我使用Intent在應用程序中打開文件(因此外部存儲必須正常工作),此確切代碼運行良好。

另外請注意,我有這些權限:

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

我的代碼

public static void storeImageToSystemGalery (String image_fullPathFileName) { 
    try { 
     Log.e(TAG, "Pp: CALLED: storeImageToSystemGalery (image_fullPathFileName=" + image_fullPathFileName +")"); 

     java.io.File internal_f = new java.io.File(image_fullPathFileName); 
     Log.e(TAG, "Pp: internal_f: exists test=" + internal_f.exists()); 
     Log.e(TAG, "Pp: internal_f: canRead test=" + internal_f.canRead()); 


     File external_f = new File(parentActivity.getExternalFilesDir(null), "galery_shared.jpg"); 
     Log.e(TAG, "Pp: external_f.AbsolutePath=" + external_f.getAbsolutePath()); 
     _f_copy(internal_f, external_f); 
     Log.e(TAG, "Pp: external_f: exists test=" + external_f.exists()); 
     Log.e(TAG, "Pp: external_f: canRead test=" + external_f.canRead()); 

     Uri imageURI = Uri.fromFile(external_f); 

     android.graphics.Bitmap bmp = android.provider.MediaStore.Images.Media.getBitmap(parentActivity.getContentResolver(), imageURI); 
     android.provider.MediaStore.Images.Media.insertImage(parentActivity.getContentResolver(), bmp, "Prstynek", "Muj prstynek"); 

    } catch (Exception ex) { 
     Log.w(TAG, "FAILED to storeImageToSystemGalery: " + ex); 
     ex.printStackTrace(); 
    } 
} 


static private void _f_copy(File fin, File dst) throws IOException { 
    FileInputStream in=new FileInputStream(fin); 
    FileOutputStream out=new FileOutputStream(dst); 
    byte[] buf=new byte[1024]; 
    int len; 

    while ((len=in.read(buf)) > 0) { 
     out.write(buf, 0, len); 
    } 

    in.close(); 
    out.close(); 
} 

日誌轉儲:

E/cocos2d.sharkintelligence.Helper__sharkCore(5824): Pp: CALLED: storeImageToSystemGalery (image_fullPathFileName=/data/data/com.sharkintelligence.test.trivialdrive2/files/product_preview_shared.jpg) 
E/cocos2d.sharkintelligence.Helper__sharkCore(5824): Pp: internal_f: exists test=true 
E/cocos2d.sharkintelligence.Helper__sharkCore(5824): Pp: internal_f: canRead test=true 
E/cutils ( 166): Failed to mkdirat(/Removable/MicroSD): Read-only file system 
W/ContextImpl(5824): Failed to ensure directory: /Removable/MicroSD/Android/data/com.sharkintelligence.test.trivialdrive2/files 
E/cocos2d.sharkintelligence.Helper__sharkCore(5824): Pp: external_f.AbsolutePath=/storage/emulated/0/Android/data/com.sharkintelligence.test.trivialdrive2/files/galery_shared.jpg 
W/Vold ( 166): Returning OperationFailed - no handler for errno 30 
E/cocos2d.sharkintelligence.Helper__sharkCore(5824): Pp: external_f: exists test=true 
E/cocos2d.sharkintelligence.Helper__sharkCore(5824): Pp: external_f: canRead test=true 
D/dalvikvm(5824): GC_CONCURRENT freed 2216K, 24% free 7501K/9764K, paused 3ms+4ms, total 15ms 
D/dalvikvm(5824): WAIT_FOR_CONCURRENT_GC blocked 15ms 
I/dalvikvm-heap(5824): Grow heap (frag case) to 11.358MB for 4187152-byte allocation 
D/dalvikvm(5824): GC_CONCURRENT freed 138K, 18% free 11452K/13856K, paused 1ms+3ms, total 7ms 
E/MediaStore(5824): Failed to insert image 
E/MediaStore(5824): java.io.FileNotFoundException: No such file or directory 
E/MediaStore(5824): at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146) 
E/MediaStore(5824): at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:611) 
E/MediaStore(5824): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:922) 
E/MediaStore(5824): at android.content.ContentResolver.openOutputStream(ContentResolver.java:669) 
E/MediaStore(5824): at android.content.ContentResolver.openOutputStream(ContentResolver.java:645) 
E/MediaStore(5824): at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:912) 
E/MediaStore(5824): at com.sharkintelligence.android.app.Helper__sharkCore.storeImageToSystemGalery(Helper__sharkCore.java:295) 
E/MediaStore(5824): at org.cocos2dx.lib.Cocos2dxRenderer.nativeRender(Native Method) 
E/MediaStore(5824): at org.cocos2dx.lib.Cocos2dxRenderer.onDrawFrame(Cocos2dxRenderer.java:91) 
E/MediaStore(5824): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1531) 
E/MediaStore(5824): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1248) 
D/MediaProvider( 787): object removed 188 
+0

如果你看看你的logcat的第一行,你知道爲什麼。 – greenapps

+0

建議給_f_copy一個布爾返回值並在使用後檢查它。 – greenapps

+0

此外,你不清楚你的頌歌流量有多遠,是否有例外/捕捉。 – greenapps

回答

相關問題