相关疑难解决方法(0)

如何从Uri获取位图?

如何从Uri中获取Bitmap对象(如果我成功将其存储在 /data/data/MYFOLDER/myimage.png或中file///data/data/MYFOLDER/myimage.png)以在我的应用程序中使用它?

有没有人知道如何实现这一目标?

android uri android-image android-bitmap

187
推荐指数
10
解决办法
30万
查看次数

Android - 减少图像文件大小

我有一个URI图像文件,我想减小它的大小上传它.初始图像文件大小取决于从移动设备到移动设备(可以是2MB,可以是500KB),但我希望最终大小约为200KB,以便我可以上传它.
从我读到的,我有(至少)2个选择:

  • 使用BitmapFactory.Options.inSampleSize,对原始图像进行二次采样并获得较小的图像;
  • 使用Bitmap.compress压缩指定压缩质量的图像.

什么是最好的选择?


我想最初调整图像宽度/高度,直到宽度或高度高于1000px(类似1024x768或其他),然后压缩图像质量下降,直到文件大小超过200KB.这是一个例子:

int MAX_IMAGE_SIZE = 200 * 1024; // max final file size
Bitmap bmpPic = BitmapFactory.decodeFile(fileUri.getPath());
if ((bmpPic.getWidth() >= 1024) && (bmpPic.getHeight() >= 1024)) {
    BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
    bmpOptions.inSampleSize = 1;
    while ((bmpPic.getWidth() >= 1024) && (bmpPic.getHeight() >= 1024)) {
        bmpOptions.inSampleSize++;
        bmpPic = BitmapFactory.decodeFile(fileUri.getPath(), bmpOptions);
    }
    Log.d(TAG, "Resize: " + bmpOptions.inSampleSize);
}
int compressQuality = 104; // quality decreasing by 5 every loop. (start from 99)
int streamLength = …
Run Code Online (Sandbox Code Playgroud)

android bitmap image-uploading image-resizing

57
推荐指数
1
解决办法
9万
查看次数

BitmapFactory.decodeFile内存不足,图像为2400x2400

我需要将文件从文件发送到服务器.服务器以2400x2400的分辨率请求图像.

我想要做的是:

1)使用正确的inSampleSize使用BitmapFactory.decodeFile获取位图.

2)以JPEG格式压缩图像,质量为40%

3)在base64中对图像进行编码

4)发送到服务器

我无法实现第一步,它抛出一个内存不足的异常.我确信inSampleSize是正确的,但我想即使使用inSampleSize,Bitmap也很大(在DDMS中大约30 MB).

任何想法怎么办呢?我可以在不创建位图对象的情况下执行这些步骤吗?我的意思是在文件系统而不是RAM内存上进行.

这是当前的代码:

// The following function calculate the correct inSampleSize
Bitmap image = Util.decodeSampledBitmapFromFile(imagePath, width,height);   
// compressing the image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 40, baos);
// encode image
String encodedImage = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT));


public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > …
Run Code Online (Sandbox Code Playgroud)

android bitmap

16
推荐指数
1
解决办法
2万
查看次数

当我上传到服务器时相机图像会旋转

我要么拍照,要么从图库中选择一张照片,然后按应有的方式在 ImageView 中显示它(就旋转而言)。但是,每当我将其上传到服务器时,它总是以横向模式上传,即使它在我的画廊中处于纵向模式。我该如何解决这个问题?

private void takePhoto() {
    Intent takePhoto = new Intent();
    takePhoto.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    File photoFile = null;
    try {
        photoFile = imagePath();
    } catch (IOException e) {
        Log.d(TAG, "Take Photo: " + e.getMessage());
    }
    takePhoto.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
    startActivityForResult(takePhoto, REQUEST_IMAGE);
}

private File imagePath() throws IOException {
    String timeStamp = new java.text.SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String imageFileName = "IMAGE_" + timeStamp + "_";
    File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(imageFileName, ".jpg", storageDirectory);
    mImageLocation = image.getAbsolutePath();
    return image;
}

private void uploadMultipart() { …
Run Code Online (Sandbox Code Playgroud)

android multipartform-data image-rotation

7
推荐指数
1
解决办法
8708
查看次数

发布之前减少图片的大小

我有一个函数将文件(来自相机或图库的图片)发送到WebService.我想减少fileUri帖子前的图像大小(每个例子50%).该文件是图库或相机图像.

这是我的postFile功能:

public static void postFile(Context context, String url, String fileUri, AsyncHttpResponseHandler responseHandler) {
    if (myCookieStore == null)
    {
        myCookieStore = new PersistentCookieStore(context);
        client.setCookieStore(myCookieStore);
    }

    File myFile = new File(Uri.parse(fileUri).getPath());

    RequestParams params = new RequestParams();
    try {
        params.put("profile_picture", myFile);
    } catch(FileNotFoundException e) {
        Log.d("error", "error catch");
    }
    Log.d("absolute url", "" + "*" + getAbsoluteUrl(url) + "*");
    client.post(context, getAbsoluteUrl(url), params, responseHandler);
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点 ?

android android-studio

5
推荐指数
1
解决办法
2338
查看次数

[Android] -Resize image上传到服务器

我的服务器限制图像大小上传图像(2MB).我想将图像从Android设备上传到服务器.我想调整图像大小.什么是我可以调整图像大小的最佳方法?

android

4
推荐指数
1
解决办法
9992
查看次数

如何将位图保存到Firebase

我创建了一个简单的应用程序来裁剪图像.现在我想将此图像保存到Fire基座.

photo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Intent imageDownload = new 
Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      Intent imageDownload=new Intent();
      imageDownload.setAction(Intent.ACTION_GET_CONTENT);
      imageDownload.setType("image/*");
      imageDownload.putExtra("crop", "true");
      imageDownload.putExtra("aspectX", 1);
      imageDownload.putExtra("aspectY", 1);
      imageDownload.putExtra("outputX", 200);
      imageDownload.putExtra("outputY", 200);
      imageDownload.putExtra("return-data", true);
      startActivityForResult(imageDownload, GALLERY_REQUEST_CODE);


        }
    });
 }
  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent 
  data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK && 
   data != null) {
        Bundle extras = data.getExtras();
        image = extras.getParcelable("data");
        photo.setImageBitmap(image);

   }






}
Run Code Online (Sandbox Code Playgroud)

如何将此图像保存到Firebase.我尝试了很多教程,但没能成功.请用简单的代码验证.

android firebase android-bitmap firebase-storage

2
推荐指数
2
解决办法
2万
查看次数