0

我有一个 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。问题是,有时这段代码工作正常,有时它给我这个错误。

4

2 回答 2

0

发生的事情是我的活动中有多个 ID 为 0 的 LoaderManager,因此查询返回了之前的列(这只是照片名称,没有其他内容,感谢 Michael 查看我的列名称的提示)

于 2012-07-30T14:51:19.390 回答
0

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

您可以使用您的getColumnNames方法Cursor来获取您实际从MediaStore. 希望这将为正在发生的事情提供一些提示。

于 2012-07-30T15:02:37.603 回答