2016-09-07 184 views
0

我搜索了很多天约​​我承认,我发现了很多的代码,但没有人工作。 我现在有这个代码,当我点击按钮,并选择一个图像的程序停止“不幸的应用程序已停止”。 任何帮助将不胜感激...... 这是代码处理结果从图库

public class MainActivity extends AppCompatActivity { 

private static int RESULT_LOAD_IMAGE = 1; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button buttonLoadImage = (Button) findViewById(R.id.button); 
    buttonLoadImage.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Intent i = new Intent(
        Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

      startActivityForResult(i, RESULT_LOAD_IMAGE); 
     } 
    }); 
} 


@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 selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     Cursor cursor = getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 

     ImageView imageView = (ImageView) findViewById(R.id.ImageView); 
     imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
    } 
} 

}

回答

0

下面是从库中选择一个图片或捕获一个从摄像头和一个ImageView的使用它的工作方案。

来源:SO

首先在onCreate方法初始化ImageView的。然后通过任何按钮的onClick事件调用selectimage函数,就是这样。

//functions to select image from the device 
    private void selectImage() { 
     final CharSequence[] items = {"Take Photo","Choose from Library", "Cancel" }; 
     AlertDialog.Builder builder = new AlertDialog.Builder(signature_new.this); 
     builder.setTitle("Add Photo!"); 
     builder.setItems(items, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int item) { 
       if (items[item].equals("Take Photo")) { 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(intent, REQUEST_CAMERA); 
       } else if (items[item].equals("Choose from Library")) { 
        Intent intent = new Intent(
          Intent.ACTION_PICK, 
          android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        intent.setType("image/*"); 
        startActivityForResult(
          Intent.createChooser(intent, "Select File"), 
          SELECT_FILE); 
       } else if (items[item].equals("Cancel")) { 
        dialog.dismiss(); 
       } 
      } 
     }); 
     builder.show(); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK) { 
      if (requestCode == REQUEST_CAMERA) { 
       Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
       ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
       thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes); 

       File destination = new File(Environment.getExternalStorageDirectory(), 
         System.currentTimeMillis() + ".jpg"); 

       FileOutputStream fo; 
       try { 
        destination.createNewFile(); 
        fo = new FileOutputStream(destination); 
        fo.write(bytes.toByteArray()); 
        fo.close(); 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 


       imageF.setImage(ImageSource.bitmap(thumbnail)); 

      } else if (requestCode == SELECT_FILE) { 
       Uri selectedImageUri = data.getData(); 
       try { 

        Bitmap bm=decodeUri(selectedImageUri); 
        imageViewF.setImage(ImageSource.bitmap(bm)); 
        //uploadbm=bm; 
        //dialog_dimension(); 
       } 
       catch(FileNotFoundException e) { 
        e.printStackTrace(); 
       } 

      } 
     } 
    } 
+0

感谢男士很多,我需要一些帮助,因为出错了,有一些样品不行> –