2016-09-07 181 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

感謝男士很多,我需要一些幫助,因爲出錯了,有一些樣品不行> –