Java – images that need to select code from the camera and Gallery – applies to all Android phones
•
Java
private void selectImage() {
private void selectImage() { final CharSequence[] items = { "Take Photo","Choose from Library","Cancel" }; AlertDialog.Builder builder = new AlertDialog.Builder( PostProperty2Activity.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); File f = new File(android.os.Environment .getExternalStorageDirectory(),"temp.jpg"); Log.d("File f ","" + Uri.fromFile(f)); intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(f)); 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); Bitmap.CompressFormat.JPEG.toString(); 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) { File f = new File(Environment.getExternalStorageDirectory() .toString()); Log.d("File fd ","" + Uri.fromFile(f)); Log.d("getactivity f ","" + f); Log.d("flistfile f ","" + f.listFiles()); for (File temp : f.listFiles()) { if (temp.getName().equals("temp.jpg")) { f = temp; break; } } try { Bitmap bm; BitmapFactory.Options btmapOptions = new BitmapFactory.Options(); bm = BitmapFactory.decodeFile(f.getAbsolutePath(),btmapOptions); // bm = Bitmap.createScaledBitmap(bm,70,true); pdialog.show(); profileimage.setImageBitmap(bm); String path = android.os.Environment .getExternalStorageDirectory() + File.separator + "Upostr"; f.delete(); File wallpaperDirectory = new File(path); wallpaperDirectory.mkdirs(); OutputStream fOut = null; File file = new File(wallpaperDirectory,String.valueOf(System.currentTimeMillis()) + ".jpg"); imagepath = file.getAbsolutePath(); try { fOut = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG,85,fOut); fOut.flush(); fOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } imagevalue = "image"; uploadImageToServer(); // upload(imagepath); } else if (requestCode == SELECT_FILE) { Uri selectedImageUri = data.getData(); Log.d("uri","" + selectedImageUri); String tempPath = getPath(selectedImageUri); Bitmap bm; BitmapFactory.Options btmapOptions = new BitmapFactory.Options(); bm = BitmapFactory.decodeFile(tempPath,btmapOptions); pdialog.show(); profileimage.setImageBitmap(bm); imagepath = getRealPathFromURI(selectedImageUri); imagevalue = "image"; uploadImageToServer(); // upload(imagepath); } } }
This code is applicable to smashing or Sony, but not Intel and some other Android devices If I add clipping to this code, it will also crash in Sony
Any way to solve this problem?
Solution
if (options[item].equals("Choose from Gallery")) {
if (options[item].equals("Choose from Gallery")) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Intent photoPickerIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI); photoPickerIntent.setType("image/*"); photoPickerIntent.addCategory(photoPickerIntent.CATEGORY_OPENABLE); startActivityForResult(photoPickerIntent,2); } else if (options[item].equals("Cancel")) { dialog.dismiss(); } else { Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI); img_path = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString(); startActivityForResult(intent,2); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码