2016-08-16 163 views
0

我嘗試從相機發送圖像到我的活動。我叫startActivityForResult從適配器這樣的:startActivityForResult返回數據null

photo.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          dialog.dismiss(); 
          mActivity.dispatchTakePictureIntent(part.getId()); 
          } 
       }); 

這裏有一個活動是方法:

public void dispatchTakePictureIntent(String id) { 
     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    // Ensure that there's a camera activity to handle the intent 
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     // Create the File where the photo should go 
     File photoFile = null; 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
      // Error occurred while creating the File 

     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      Uri photoURI = FileProvider.getUriForFile(this, 
        "com.example.eltegps011.eltegps.fileprovider", 
        photoFile); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); 
      takePictureIntent.putExtra("Id",id); 

      startActivityForResult(takePictureIntent, CAM_REQUEST); 
     } 

    } 
} 

,這是我的onActivityResult();

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == 2) { 
      part = (Part) data.getSerializableExtra("Part"); 
      Log.e(TAG, "onActivityResult: "); 

    }if(requestCode == 1313) { 
     Bitmap thumbnail = (Bitmap)data.getExtras().get("data"); 
     String id = data.getStringExtra("Id"); 
     Log.e(TAG, "onActivityResult: " + id); 
    } 

然而,位圖的縮略圖=(位圖)data.getExtras()。獲得( 「數據」)爲空並且resultCode爲始終是-1。它看起來像意圖不去活動。任何想法爲什麼?

+0

後續[此](http://stackoverflow.com/a/13977619/5296734)它可幫助您找到解決方案。 –

+0

'//創建文件時發生錯誤,並且您仍然繼續,就好像什麼都沒有發生。你應該顯示吐司這樣說,然後返回。 – greenapps

+1

'photoFile = createImageFile()'。除了檢查目錄是否可寫以外,不需要創建該文件。你可以直接刪除它。將創建留給相機應用程序。 – greenapps

回答

0

所有版本的全功能演示。

對於捕獲和挑選圖像

注:點擊安卓6.0 GET運行權限,然後procced。

public class MainActivity extends Activity { 

    private ImageView camera, gallery; 
    File photoFile; 
    Uri uri; 
    private final static int RESULT_LOAD_IMAGE = 1; 
    private final static int REQUEST_IMAGE_CAPTURE = 2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     camera = (ImageView) findViewById(R.id.Img); 
     gallery = (ImageView) findViewById(R.id.imageView); 
     camera.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dispatchTakePictureIntent(); 

      } 
     }); 
     gallery.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(i, RESULT_LOAD_IMAGE); 
      } 
     }); 
    } 

    private void dispatchTakePictureIntent() { 

     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
      photoFile = Environment.getExternalStoragePublicDirectory("/myimage/save.jpg"); 
      if (photoFile != null) { 
       takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
         Uri.fromFile(photoFile)); 
       startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
      } 
     } 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
      uri = data.getData(); 
      String selecteadImage = getRealPathFromURI(this, data.getData()); 
      Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show(); 

     } else if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 

      if (data != null && data.getData() != null) { 
       String selecteadImage = getRealPathFromURI(this, data.getData()); 
       Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show(); 
      } else { 
       if (photoFile != null) { 
        String selecteadImage = photoFile.getAbsolutePath(); 
        Toast.makeText(this, "Image path " + selecteadImage, Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
    } 

    public String getRealPathFromURI(Context context, Uri contentUri) { 
     Cursor cursor = null; 
     try { 
      String[] proj = {MediaStore.Images.Media.DATA}; 
      cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
     } finally { 
      if (cursor != null) { 
       cursor.close(); 
      } 
     } 
    } 
} 

清單權限:

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

activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/image_1"> 


    <ImageView 
     android:id="@+id/camera" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:src="@mipmap/ic_launcher" /> 

    <ImageView 
     android:id="@+id/gallery" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/camera" 
     android:layout_centerHorizontal="true" 
     android:src="@mipmap/ic_launcher" /> 
</RelativeLayout>