【问题标题】:how to capture and save HD images in android如何在android中捕获和保存高清图像
【发布时间】:2015-03-18 06:10:09
【问题描述】:
public class CameraActivity extends Activity {

ImageView imgFavorite;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
    imgFavorite = (ImageView)findViewById(R.id.imageView1);
    // set listener on image view
    imgFavorite.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
           // open cam
          open();
       }
    });
 }
 public void open(){

    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    //we will use this function startActivityForResult() to launch this activity and wait for its result.
    startActivityForResult(intent, 0);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bp = (Bitmap) data.getExtras().get("data");
     imgFavorite.setImageBitmap(bp);
 }

这是我的代码,它使用现有的相机应用程序来捕获图像.. 但它在某些 android 设备上不起作用.. 它不会将捕获的图像保存在画廊中.. 我该如何解决这个问题??

【问题讨论】:

    标签: android android-camera android-camera-intent


    【解决方案1】:

    也使用此权限:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    使用这个:

       ImageView drawingVIew = (ImageView) findViewById(R.id.drawing);//let this be your image view
              //your other code
          drawingVIew.setDrawingCacheEnabled(true);
                    String imgSaved = MediaStore.Images.Media.insertImage(
                            getContentResolver(), drawingVIew.getDrawingCache(),
                            UUID.randomUUID().toString() + ".png", "drawing");
                    if (imgSaved != null) {
                        Toast savedToast = Toast.makeText(getApplicationContext(),
                                "Drawing saved to Gallery!", Toast.LENGTH_SHORT);
                        savedToast.show();
                    } else {
                        Toast unsavedToast = Toast.makeText(getApplicationContext(),
                                "Oops! Image could not be saved.", Toast.LENGTH_SHORT);
                        unsavedToast.show();
                    }
                    drawingVIew.destroyDrawingCache();
    

    【讨论】:

    • 实际上只有高清图像没有显示在画廊中..有没有办法压缩高清图像??
    【解决方案2】:

    是的,您可以压缩文件。这取决于您使用的百分比。你应该为你想要的结果应用最好的数字。就像我在下面的代码中使用 100

    try {
            FileOutputStream fos = new FileOutputStream(yourImage);
            //here change the value from 100 to your best result of compressing
                the photo before saving . and this will also update the gallery
            newImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    
            fos.close();
            //refreshing gallery
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            mediaScanIntent.setData(Uri.fromFile(yourImage));
            sendBroadcast(mediaScanIntent);
        } catch (IOException e) {
            Toast.makeText(this, "Pic not saved", Toast.LENGTH_SHORT).show();
            return;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 2012-06-10
      • 1970-01-01
      • 2014-03-09
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多