2016-02-09 112 views
1

在这里,我不存储我捕获的图片。它在图像视图中显示,但我试图保存图像,但图像不显示在图像视图中。无法保存位图图像

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.provider.MediaStore; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.text.SimpleDateFormat; 
import java.util.Date; 


public class MainActivity extends Activity { 
private static final int CAMERA_REQUEST = 1; 
private static int RESULT_LOAD_IMG = 1; 
String imgDecodableString; 
ImageView iv; 
Bitmap bmp; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    iv = (ImageView) findViewById(R.id.imgView); 
    Button b = (Button) findViewById(R.id.buttonLoadPicture); 
    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      selectImage(); 
     } 
    }); 
} 
public void selectImage() { 

    final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" }; 

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
    builder.setTitle("Add Photo!"); 
    builder.setItems(options, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int item) { 
      if (options[item].equals("Take Photo")) { 
       String path = Environment.getExternalStorageDirectory() + "/CameraImages/example.jpg"; 
       File file = new File(path); 
       Uri outputFileUri = Uri.fromFile(file); 
       Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       // intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 

       startActivityForResult(intent, 1); 
      } else if (options[item].equals("Choose from Gallery")) { 
       Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
       startActivityForResult(intent, 2); 
      } else if (options[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 == 1) { 
     if (data != null && data.getExtras() != null) { 
      bmp = (Bitmap) data.getExtras().get("data"); 
      Log.w("path of image from gallery......******************.........", bmp + ""); 
      iv.setImageBitmap(bmp); 
     } 
}else { 
     if (requestCode == 2) { 

      Uri selectedImage = data.getData(); 
      String[] filePath = {MediaStore.Images.Media.DATA}; 
      Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null); 
      c.moveToFirst(); 
      int columnIndex = c.getColumnIndex(filePath[0]); 
      String picturePath = c.getString(columnIndex); 
      c.close(); 
      bmp = (BitmapFactory.decodeFile(picturePath)); 
      Log.w("path of image from gallery......******************.........", picturePath + ""); 
      iv.setImageBitmap(bmp); 
     } 
    } 
}}} 

当加法intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri );图像被保存但在图像视图

+0

检查这,这将帮助你在我的应用程序,其工作代码:HTTP://stackoverflow.com/questions/34923702/android-getting-pictures-from-photos-galery-error-on-some-devices/34942980#349429 80个 –

+0

嗨苏哈斯,UR链接是从画廊实际上多数民众赞成在为我工作我想是我需要甚至可以保存图像时我在相应的像场 –

+0

显示它是我张贴保存图像的答案提取图像。 –

回答

1

这里是我用来捕捉和保存摄像机的图像,然后显示给ImageView的代码。你可以根据你的需要使用。

你必须保存相机图像特定位置然后从该位置获取然后将其转换为字节数组。

下面是用于打开捕获相机图像活性的方法。

private static final int CAMERA_PHOTO = 111; 

private Uri imageToUploadUri; 


private void captureCameraImage() { 

     Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     File f = new File(Environment.getExternalStorageDirectory(), 
"POST_IMAGE.jpg"); 

     chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 

     imageToUploadUri = Uri.fromFile(f); 

     startActivityForResult(chooserIntent, CAMERA_PHOTO); 

    } 

那么你的onActivityResult()方法应该是这样的。

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

      if (requestCode == CAMERA_PHOTO && resultCode == Activity.RESULT_OK) { 
       if(imageToUploadUri != null){ 
        Uri selectedImage = imageToUploadUri; 
        getContentResolver().notifyChange(selectedImage, null); 
        Bitmap reducedSizeBitmap = getBitmap(imageToUploadUri.getPath()); 
        if(reducedSizeBitmap != null){ 
         ImgPhoto.setImageBitmap(reducedSizeBitmap); 
         Button uploadImageButton = (Button) findViewById(R.id.uploadUserImageButton); 
          uploadImageButton.setVisibility(View.VISIBLE);     
        }else{ 
         Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show(); 
        } 
       }else{ 
        Toast.makeText(this,"Error while capturing Image",Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 

这里是onActivityResult()中使用的getBitmap()方法。在获取相机捕获图像位图的同时,我已经完成了所有可能的性能改进。

private Bitmap getBitmap(String path) { 

     Uri uri = Uri.fromFile(new File(path)); 
     InputStream in = null; 
     try { 
      final int IMAGE_MAX_SIZE = 1200000; // 1.2MP 
      in = getContentResolver().openInputStream(uri); 

      // Decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(in, null, o); 
      in.close(); 


      int scale = 1; 
      while ((o.outWidth * o.outHeight) * (1/Math.pow(scale, 2)) > 
        IMAGE_MAX_SIZE) { 
       scale++; 
      } 
      Log.d("", "scale = " + scale + ", orig-width: " + o.outWidth + ", orig-height: " + o.outHeight); 

      Bitmap b = null; 
      in = getContentResolver().openInputStream(uri); 
      if (scale > 1) { 
       scale--; 
       // scale to max possible inSampleSize that still yields an image 
       // larger than target 
       o = new BitmapFactory.Options(); 
       o.inSampleSize = scale; 
       b = BitmapFactory.decodeStream(in, null, o); 

       // resize to desired dimensions 
       int height = b.getHeight(); 
       int width = b.getWidth(); 
       Log.d("", "1th scale operation dimenions - width: " + width + ", height: " + height); 

       double y = Math.sqrt(IMAGE_MAX_SIZE 
         /(((double) width)/height)); 
       double x = (y/height) * width; 

       Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, 
         (int) y, true); 
       b.recycle(); 
       b = scaledBitmap; 

       System.gc(); 
      } else { 
       b = BitmapFactory.decodeStream(in); 
      } 
      in.close(); 

      Log.d("", "bitmap size - width: " + b.getWidth() + ", height: " + 
        b.getHeight()); 
      return b; 
     } catch (IOException e) { 
      Log.e("", e.getMessage(), e); 
      return null; 
     } 
    } 

还可以确保您添加的所有权限清单档案中的

希望这将帮助您

+0

让我知道你的结果 – saeed

+0

嗨赛义德我们的代码是伟大的工作,但我需要小的帮助,他的图像只有一次下一次保存时,我拍照的overlaping的以前的图像,你可以告诉我如何单独列出它 –

+0

其简单,你可以添加timstamp来保存图像 – saeed

0

显示不试试这个:

FileOutputStream out = null; 
     String filename = getFilename(); 
     try { 
      out = new FileOutputStream(filename); 
      scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 




public static String getFilename() { 
     File file = new File(dirPath,image_path+"/Images"); 
     if (!file.exists()) { 
      file.mkdirs(); 
     } 
     String uriSting = (file.getAbsolutePath() + "/"+ System.currentTimeMillis() + ".jpg"); 
     return uriSting; 

    } 
0

更改onActivityResult代码和声明outputFileUri全球...

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 1) { 

      BitmapFactory.Options options=new BitmapFactory.Options(); 
      options.inSampleSize=8; 
      final Bitmap bitmap=BitmapFactory.decodeFile(outputFileUri.getPath(),options); 
      Bitmap photo = bitmap; 

       //Log.w("path of image from gallery......******************.........", bmp + ""); 
       iv.setImageBitmap(photo); 

     }else { 
      if (requestCode == 2) { 

       Uri selectedImage = data.getData(); 
       String[] filePath = {MediaStore.Images.Media.DATA}; 
       Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null); 
       c.moveToFirst(); 
       int columnIndex = c.getColumnIndex(filePath[0]); 
       String picturePath = c.getString(columnIndex); 
       c.close(); 
       bmp = (BitmapFactory.decodeFile(picturePath)); 
       // Log.w("path of image from gallery......******************.........", picturePath + ""); 
       iv.setImageBitmap(bmp); 
      } 
     } 
    }}