使用自定义名称将图像保存到自定义文件夹

时间:2015-09-28 10:41:42

标签: android camera

我正在尝试构建一个捕获图像的应用程序,并将图像保存到具有自定义名称的自定义文件夹,但我无法执行它。

这是我写的代码,请仔细阅读代码,并在我错误的地方纠正我。

public class MainActivity extends Activity {
    private static final int SELECT_PICTURE = 1;
    private String selectedImagePath;
    private static final int CAMERA_REQUEST = 1888;
    private ImageView imageView;
    private Builder alertDialogBuilder;

    final Context context = this;
    String name_str;

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

        imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        Button searchButton = (Button) this.findViewById(R.id.button2);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);

               }
        });
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
            }
        });
    }
    public void onActivityResult1(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                imageView.setImageURI(selectedImageUri);
            }
        }
    }
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        final Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);

        //Creating Alert Dialog Box
        AlertDialog.Builder askname = new AlertDialog.Builder(MainActivity.this);
        final EditText inputname = new EditText(this);
        askname.setMessage("Enter Name");
        askname.setView(inputname);

        askname.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                    @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
             name_str = inputname.getEditableText().toString();
                Toast.makeText(context,name_str,Toast.LENGTH_LONG).show();
                dialog.dismiss();

                /*if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
                    Bitmap photo = (Bitmap) data.getExtras().get("data");
                    //photo = BitmapFactory.decodeByteArray(data,0,data.length);
                    imageView.setImageBitmap(photo);*/

                    String file ="/DCIM/VisitingCards/";
                    File folder = new File(Environment.getExternalStorageDirectory() + file);

                    System.out.println("File name is :"+folder);
                    boolean success = true;
                    if (!folder.exists()) {
                        success = folder.mkdir();
                    }
                    if (success) {
                        //ON success
                        System.out.println("Directory Present");
                        System.out.println("In Success Case");
                         try {
                                FileOutputStream out = new FileOutputStream(folder);
                                photo.compress(Bitmap.CompressFormat.JPEG, 100, out);
                                out.flush();
                                out.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                      } else {
                        //ON failure
                        System.out.println("Directory not Present");
                        System.out.println("In Failure Case");
                    }
                  System.out.println("Before the Dialog builder");
            }
        });
            askname.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
              // Canceled.
            dialog.dismiss();
            }
         }); //End of alert.setNegativeButt


    askname.show();
        }    

    }  

}

1 个答案:

答案 0 :(得分:0)

你应该在意图之前询问图像名称,然后使用putExtra。 希望它可以帮到你