从库中复制图像,然后将其删除

时间:2012-07-26 14:04:26

标签: android android-gallery

我有一个AsyncTask,它将刚拍摄的照片复制到我指定的位置,然后原始照片被删除。但是,我一直在收到错误。

final String[] projection = { MediaStore.Images.ImageColumns.SIZE,
                MediaStore.Images.ImageColumns.DISPLAY_NAME,
                MediaStore.Images.ImageColumns.DATA,
                BaseColumns._ID,};

        u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        boolean found = false;

        if (CurrentFile != null)
        {               
            if ((u != null) && (CurrentFile.length() > 0))
            {
                Looper.prepare();   
                getSupportLoaderManager().initLoader(0, null, new LoaderCallbacks<Cursor>(){

                    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
                        CursorLoader loader = new CursorLoader(ExpenseReport.this, u, projection, null, null, null);
                        return loader;
                    }

                    public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
                        if ((c != null) && (c.moveToFirst())){

                            do 
                            {

                                for (String sGallery : GlobalInfo.GalleryList){

                                    if (sGallery.equalsIgnoreCase(c.getString(1))){
                                        found = true;
                                        break;
                                    }
                                } 
                                if (!found){
                                    File f = new File(c.getString(2));


                                    if ((f.exists()) && (CurrentFile.length() < c.getLong(0)) && (CurrentFile.delete())){

                                        try
                                        {
                                            CurrentFile.createNewFile();
                                            FileChannel source = null;
                                            FileChannel destination = null;
                                            try{
                                                source = new FileInputStream(f).getChannel();
                                                destination = new FileOutputStream(CurrentFile).getChannel();
                                                destination.transferFrom(source, 0, source.size());
                                            }
                                            finally{

                                                if (source != null)
                                                    source.close();

                                                if (destination != null)
                                                    destination.close();
                                            }
                                        }
                                        catch (IOException e)
                                        {
                                            e.printStackTrace();
                                            GlobalInfo.LOG("", "Couldn't Copy The File");

                                        }
                                    }
                                    ContentResolver cr = getContentResolver();
                                    Bitmap bitmap=null;
                                    try {
                                        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, CurrentUri);
                                    } catch (FileNotFoundException e1) {
                                        e1.printStackTrace();

                                    } catch (IOException e1) {
                                        e1.printStackTrace();

                                    }
                                    if(bitmap.getWidth()<2000) 
                                        bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), false);
                                    else
                                        bitmap = Bitmap.createScaledBitmap(bitmap, 2000, 2000, false);

                                    try {
                                        FileOutputStream out = new FileOutputStream(CurrentFile);
                                        bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
                                    } catch (Exception e) {
                                        e.printStackTrace();

                                    }

                                    cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                                            BaseColumns._ID + "=" + c.getString(3), null);

                                    break;                        
                                }
                            } 
                            while (c.moveToNext());
                        }
                    }

                    public void onLoaderReset(Loader<Cursor> arg0) {                        
                    }

                });
                Looper.loop();
            }
        }

这是我得到的错误:

07-26 09:38:58.060: E/CursorWindow(1566): Bad request for field slot 0,1. numRows = 14, numColumns = 1
07-26 09:38:58.107: W/dalvikvm(1566): threadid=10: thread exiting with uncaught exception (group=0x40015578)
07-26 09:38:58.142: E/AndroidRuntime(1566): FATAL EXCEPTION: AsyncTask #1
07-26 09:38:58.142: E/AndroidRuntime(1566): java.lang.RuntimeException: An error occured while executing doInBackground()
07-26 09:38:58.142: E/AndroidRuntime(1566):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at java.lang.Thread.run(Thread.java:1019)
07-26 09:38:58.142: E/AndroidRuntime(1566): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 1 failed
07-26 09:38:58.142: E/AndroidRuntime(1566):     at android.database.CursorWindow.getString_native(Native Method)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at android.database.CursorWindow.getString(CursorWindow.java:361)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at android.database.CursorWrapper.getString(CursorWrapper.java:135)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at com.report.expense.ExpenseReport$FinishPhotoTask$1.onLoadFinished(ExpenseReport.java:1131)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at com.report.expense.ExpenseReport$FinishPhotoTask$1.onLoadFinished(ExpenseReport.java:1)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:425)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:559)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at com.report.expense.ExpenseReport$FinishPhotoTask.doInBackground(ExpenseReport.java:1115)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at com.report.expense.ExpenseReport$FinishPhotoTask.doInBackground(ExpenseReport.java:1)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at     android.os.AsyncTask$2.call(AsyncTask.java:185)
07-26 09:38:58.142: E/AndroidRuntime(1566):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
07-26 09:38:58.142: E/AndroidRuntime(1566):     ... 4 more

(ExpenseReport.java:1115):

getSupportLoaderManager().initLoader(0, null, new LoaderCallbacks<Cursor>(){

(ExpenseReport.java:1131):

if (sGallery.equalsIgnoreCase(c.getString(1))){

编辑: 我正在尝试通过比较拍照前后的照片名称列表来获取刚拍摄的照片的大小,名称,位置和ID。问题是,有时这个代码工作正常,有时它给我这个错误。

2 个答案:

答案 0 :(得分:0)

发生的事情是我的活动中有多个LoaderManager,ID为0,所以查询返回之前的列(这只是照片名称而没有别的,感谢Michael提示查看我的列名称)

答案 1 :(得分:0)

(根据OP的建议从我的评论中创建答案):

您可以使用getColumnNames上的Cursor方法获取MediaStore实际获得的列的列表。希望这会提供一些正在发生的事情。