【问题标题】:Saving Image path of pictures taken to Database将拍摄的图片的图像路径保存到数据库
【发布时间】:2014-09-17 09:09:37
【问题描述】:

我是安卓开发新手。我正在尝试创建一个应用程序,当用户拍照时,它的路径将保存到数据库中。我可以从图库中保存和检索图像,但是如果它是由相机拍摄的,我该如何保存?

这是我的代码

private void selectImage() {
                final CharSequence[] items = { "Take Photo", "Choose from Library",
                        "Cancel" };

                AlertDialog.Builder builder = new AlertDialog.Builder(Student_Profile.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");

                           intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                           startActivityForResult(intent, 1);

                        } 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"),
                                    0);
                        } else if (items[item].equals("Cancel")) {
                            dialog.dismiss();
                        }
                    }
                });
                builder.show();
            }

        @Override
            public void onActivityResult( int requestCode, int resultCode, Intent data) {
                 super.onActivityResult(requestCode, resultCode, data);


                 if (resultCode == Activity.RESULT_OK){
                    if (requestCode == 0) {
                        studentsDbAdapter.delete_Pic_byID(studID);
                     Uri targetUri = data.getData();
                     picture_location=getPath(targetUri);
                    // picture_location = targetUri.toString();             
                     studentsDbAdapter.insertImagePath(studID ,picture_location);
                     showpic();
                 }   }

                 else if (requestCode == 1) {
                    // How do I get The URI?
                    // I'm thinking that maybe if I can get the Uri 
                    //from the new image I can save it like how I saved the image to my DB gallery style.
                    String pic_location=getPath(Uri);
                     studentsDbAdapter.insertImagePath(studID ,pic_location);
                     showpic();
                 }
                 }

             public String getPath(Uri uri) {
                 String[] projection = { MediaStore.Images.Media.DATA };
                 @SuppressWarnings("deprecation")
                Cursor cursor = managedQuery(uri, projection, null, null, null);
                 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                 cursor.moveToFirst();
                 return cursor.getString(column_index);
             }

            public void showpic() {

                //LoginDataBaseAdapter db = studentsDbAdapter.open();
                 boolean emptytab = false;
                 boolean empty = studentsDbAdapter.checkPic(null, emptytab);

                //Cursor cursor = loginDataBaseAdapter.fetchProfileImageFromDatabase();  

                 if(empty==false)  
                  {  



                    String pathName = studentsDbAdapter.getImapath(studID);
                    File image = new  File(pathName); 
                    if(image.exists()){
                             ImageView imageView= (ImageView) findViewById(R.id.studpic);
                             imageView.setImageBitmap(BitmapFactory.decodeFile(image.getAbsolutePath()));
                }
                  }
                 } 

【问题讨论】:

  • 看看这个post
  • Uri = intent.getData();
  • @deniz 我已经尝试过了,但它给了我错误 09-17 17:44:11.238: E/AndroidRuntime(26174): FATAL EXCEPTION: main 09-17 17:44:11.238: E/ AndroidRuntime(26174): java.lang.RuntimeException: 将结果 ResultInfo{who=null, request=1, result=0, data=null} 传递到活动 {com.csu.eclassrecord/com.csu.eclassrecord.Student_Profile} 失败: java.lang.NullPointerException 09-17 17:44:11.238: E/AndroidRuntime(26174): 在 android.app.ActivityThread.deliverResults(ActivityThread.java:3513)
  • 检查下面的答案可能对你有帮助

标签: android


【解决方案1】:

试试这个。可能对你有帮助。

if (fromGallery) {

   data.getData()); //get image data like this

} else {

bm = (Bitmap) data.getParcelableExtra("data");

}

【讨论】:

    猜你喜欢
    • 2018-07-11
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多