2016-02-09 110 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); 
      } 
     } 
    }}