2017-02-22 67 views
0

我在我的android活動中使用AsyncTask來通過內置攝像頭捕獲圖像,並在圖像view.but上顯示它,但發生錯誤? O不知道如何解決它。請給出一些解決方案。如何使用Asynctask在imageview上捕獲和顯示圖像?

這是我的代碼:

public class MainActivity extends Activity { 
    final static int REQUEST_CODE = 1; 
    Uri imageuri = null; 
    String ImageID = null; 
    MainActivity mainActivity = null; 
    public static ImageView imageview = null; 

    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ImageButton btn = (ImageButton)findViewById(R.id.imageButton); 
     imageview = (ImageView)findViewById(R.id.imageView); 


     btn.setOnClickListener(new View.OnClickListener() { 

      //Camera Intent 
      @Override 
       public void onClick(View v) { 


       String filename = "awais.jpg"; 

       ContentValues val = new ContentValues(); 
       val.put(MediaStore.Images.Media.TITLE, filename); 

       imageuri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, val); 
       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

       startActivityForResult(intent, REQUEST_CODE); 

      } 
     }); 



    } 
    @Override 
     protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { 

      // Toast.makeText(this,"o0k",Toast.LENGTH_SHORT).show(); 


      ImageID = Converturi(imageuri, mainActivity); 

      new loadphoto().execute("" + ImageID) 
       ; 

     } 

    } 

    public static String Converturi(Uri Imageuri, Activity activity) { 

     Cursor cursor = null; 
     int imageid = 0; 

     try { 

      String proj[] = { 
       MediaStore.Images.Media._ID, 
       MediaStore.Images.Thumbnails._ID 
      }; 

      cursor = activity.managedQuery(Imageuri, proj, null, null, null); 

      int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); 
      int thumb = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 

      //int size=cursor.getCount(); 
      int thumbid = 0; 

      if (cursor.moveToFirst()) { 

       imageid = cursor.getInt(index); 
       thumbid = cursor.getInt(thumb); 
      } 

     } 
     finally { 
      if (cursor != null) { 
       cursor.close(); 
      } 
     } 
     return "" + imageid; 
    } 

    public class loadphoto extends AsyncTask<String, Void, Void> { 
     ProgressDialog dailog = new ProgressDialog(MainActivity.this); 
     Bitmap mainbitmap; 


     @Override 
      protected void onPreExecute() { 

      dailog.setMessage("Loading"); 
      dailog.show(); 

     } 

     @Override 
      protected Void doInBackground(String... urls) { 

      Bitmap bitmap = null; 
      Bitmap newbitmap = null; 

      Uri uri = null; 

      try { 


       uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + urls[0]); 
       bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); 

       if (bitmap != null) { 
        newbitmap = Bitmap.createScaledBitmap(bitmap, 170, 170, true); 
        bitmap.recycle(); 

        if (newbitmap != null) { 
         mainbitmap = newbitmap; 
        } 
       } 
      } 
      catch (IOException e) { 
       cancel(true); 
      } 


      return null; 
     } 
     @Override 
      protected void onPostExecute(Void unused) { 

      dailog.dismiss(); 
      if (mainbitmap != null) { 

       imageview.setImageBitmap(mainbitmap); 
      } 
     } 
    } 

錯誤:

E/AndroidRuntime: FATAL EXCEPTION: main 
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.itinternee.camera/com.example.itinternee.camera.MainActivity}: java.lang.NullPointerException 
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3367) 
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410) 
     at android.app.ActivityThread.access$1100(ActivityThread.java:141) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5103) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:525) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
     at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.NullPointerException 
     at com.example.itinternee.camera.MainActivity.Converturi(MainActivity.java:93) 
     at com.example.itinternee.camera.MainActivity.onActivityResult(MainActivity.java:72) 
     at android.app.Activity.dispatchActivityResult(Activity.java:5322) 
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3363) 
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3410)  
     at android.app.ActivityThread.access$1100(ActivityThread.java:141)  
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)  
     at android.os.Handler.dispatchMessage(Handler.java:99)  
     at android.os.Looper.loop(Looper.java:137)  
     at android.app.ActivityThread.main(ActivityThread.java:5103)  
     at java.lang.reflect.Method.invokeNative(Native Method)  
     at java.lang.reflect.Method.invoke(Method.java:525)  
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)  
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)  
     at dalvik.system.NativeStart.main(Native Method)  

回答

0

我認爲你不需要你的異步函數加載圖片在ImageView的.... 首先用途代碼啓動相機,然後進入

btn.setOnClickListener(new View.OnClickListener() { 
     //Camera Intent 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(intent, 0); 
     } 
    }); 

OnActivityResult添加此項。

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     Bitmap imageCapture = (Bitmap) data.getExtras().get("data"); 
     imageview.setImageBitmap(imageCapture); 

    } catch (NullPointerException e) { 

    } 

} 
+0

那麼我可以在哪裏調用m異步?執行 –

+0

例如,您可以在onActivityResult中給他打電話,但通過他恢復BITMAP。 –

相關問題