Java 存储压缩图像
我可以在内部存储器中保存图像,但无法存储压缩图像 这是我的java代码Java 存储压缩图像,java,android,Java,Android,我可以在内部存储器中保存图像,但无法存储压缩图像 这是我的java代码 <ImageView android:id="@+id/imageView" android:layout_width="257dp" android:layout_height="300dp" app:srcCompat="@mipmap/ic_launcher" tools:layout_editor_absoluteX="32dp" tools:layout_edi
<ImageView
android:id="@+id/imageView"
android:layout_width="257dp"
android:layout_height="300dp"
app:srcCompat="@mipmap/ic_launcher"
tools:layout_editor_absoluteX="32dp"
tools:layout_editor_absoluteY="98dp"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/btnCam"
android:layout_width="94dp"
android:layout_height="36dp"
android:text="Camera"
tools:layout_editor_absoluteY="448dp"
tools:layout_editor_absoluteX="145dp"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp" />
<Button
android:id="@+id/btnComp"
android:layout_width="76dp"
android:layout_height="41dp"
android:text="compress"
tools:layout_editor_absoluteX="-9dp"
tools:layout_editor_absoluteY="-5dp"
android:layout_alignBottom="@+id/btnCam"
android:layout_alignRight="@+id/imageView"
android:layout_alignEnd="@+id/imageView" />
我使用getFileUri()将图像存储在手机的Pictures目录中,并使用getimage bytes()压缩文件,但我没有收到任何错误,也无法压缩图像。我可以保存正常图像,但无法保存压缩图像
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private Button button, buttonCompress;
private String encoded_string, image_name;
private Bitmap bitmap;
private ImageView img;
private File file;
private Uri file_uri;
private ProgressDialog mProgressDialog;
Intent data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new String[]
{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);//For writing into internal storage.
img = (ImageView) findViewById(R.id.imageView);
button = (Button) findViewById(R.id.btnCam);
buttonCompress = (Button) findViewById(R.id.btnComp);
button.setOnClickListener(this);
buttonCompress.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnCam:
Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
getFileUri();
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, file_uri);
startActivityForResult(intentCamera, 10);
break;
case R.id.btnComp:
getImageBytes(img);
getFileUri();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 10 && resultCode == RESULT_OK) {
img.setImageURI(file_uri);
Log.d("path is", file.getPath());
Toast.makeText(MainActivity.this, "Successfully Written !!", Toast.LENGTH_SHORT).show();
}
}
private void getFileUri() {
image_name = "picture1.jpg";
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + image_name
);
file_uri = Uri.fromFile(file);
Log.d("uri is", file_uri.toString());
}
private byte[] getImageBytes(@NonNull ImageView imageView) {
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bos);
return bos.toByteArray();
}
}
这是我的XML文件
<ImageView
android:id="@+id/imageView"
android:layout_width="257dp"
android:layout_height="300dp"
app:srcCompat="@mipmap/ic_launcher"
tools:layout_editor_absoluteX="32dp"
tools:layout_editor_absoluteY="98dp"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/btnCam"
android:layout_width="94dp"
android:layout_height="36dp"
android:text="Camera"
tools:layout_editor_absoluteY="448dp"
tools:layout_editor_absoluteX="145dp"
android:layout_marginLeft="31dp"
android:layout_marginStart="31dp"
android:layout_below="@+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp" />
<Button
android:id="@+id/btnComp"
android:layout_width="76dp"
android:layout_height="41dp"
android:text="compress"
tools:layout_editor_absoluteX="-9dp"
tools:layout_editor_absoluteY="-5dp"
android:layout_alignBottom="@+id/btnCam"
android:layout_alignRight="@+id/imageView"
android:layout_alignEnd="@+id/imageView" />
您还没有编写代码来保存它。尝试以下代码从imageview获取图像,压缩它,然后将其保存在存储目录中。希望它对你有用
Bitmap bitmapsave = null;
//get image bitmap from imageview
bitmapsave = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
//create folder where you want to store compressed image
File file = new File(Environment.getExternalStorageDirectory().getPath(), "/yourAppname");
if (!file.exists())
{
file.mkdirs();
}
// name that image
String filename = file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg";
FileOutputStream out = null;
try
{
out = new FileOutputStream(filename);
bitmapsave.compress(Bitmap.CompressFormat.JPEG, 50, out);
out.close();
Toast.makeText(getApplicationContext(), "saved successfully", Toast.LENGTH_SHORT).show();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
您还没有编写代码来保存它。尝试以下代码从imageview获取图像,压缩它,然后将其保存在存储目录中。希望它对你有用
Bitmap bitmapsave = null;
//get image bitmap from imageview
bitmapsave = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
//create folder where you want to store compressed image
File file = new File(Environment.getExternalStorageDirectory().getPath(), "/yourAppname");
if (!file.exists())
{
file.mkdirs();
}
// name that image
String filename = file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg";
FileOutputStream out = null;
try
{
out = new FileOutputStream(filename);
bitmapsave.compress(Bitmap.CompressFormat.JPEG, 50, out);
out.close();
Toast.makeText(getApplicationContext(), "saved successfully", Toast.LENGTH_SHORT).show();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
试试这个
private void storeImage(Bitmap image) {
if(image == null){
return;
}
try {
File pictureFile = new File ("your_path");
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG,100, fos);
fos.close();
Log.e(TAG,"FILE created");
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
} catch (Exception e){
Log.e(TAG,"Excetion in image storage "+e.toString());
}
}
试试这个
private void storeImage(Bitmap image) {
if(image == null){
return;
}
try {
File pictureFile = new File ("your_path");
FileOutputStream fos = new FileOutputStream(pictureFile);
image.compress(Bitmap.CompressFormat.PNG,100, fos);
fos.close();
Log.e(TAG,"FILE created");
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
} catch (Exception e){
Log.e(TAG,"Excetion in image storage "+e.toString());
}
}
getFIleUri()中的方法应该将图像存储在存储器中。我看到了,但您没有在该方法中存储压缩图像字节。getFIleUri()中的方法应该将图像存储在存储器中。我看到了,但您没有在该方法中存储压缩图像字节。getFIleUri()方法应将图像存储在storage/simulated/0/Pictures/picture1.jpg中。方法getFileUri()应将图像存储在storage/simulated/0/Pictures/picture1.jpg中。