将图像视图(从相机捕获)中的图像保存在内部存储器中我自定义定义的文件夹中
Save the image from an ImageView(captured from camera) in my custom defined folder in the INTERNAL MEMORY
我一直在参考各种帖子和网站,我已经尝试了 3 天,但我无法弄清楚!
我的 Activity 有 2 个按钮!第一个是捕获,第二个是保存。按下捕获按钮后,我需要启动相机,然后当我们单击图片并单击确定时,图像将设置到 activity 中的 ImageView。现在,当我单击保存按钮时,我需要将图像保存在内部存储器 中我自己的文件夹中,该文件夹必须出现在文件资源管理器中 。说我的自定义文件夹>图片>MyCapture.jpg
我尝试了几个示例,但在一个案例中我能够做的是将图像存储在默认目录中,例如下载或 DCIM 或图片。
在其他情况下,我能够将图片存储在 Android>data>com.example.shravan.camera>files>Pictures>myImage.jpg
如果我在这里发帖,我可以将图像保存在 /data/user/0/com.example.shravan.asbdbsadbsabcxscsa/app_mydir/myfile
这里我还有另一种方法 saveFile ,因为我一如既往地收到 FileNotFound 异常 ,所以正在评论它!
但是,我无法保存在我自己的自定义文件夹中。请检查代码并帮助我,如果我遗漏了什么请告诉我!
代码:
MainActivity.java
package com.example.shravan.asbdbsadbsabcxscsa;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Uri imageUri;
private static final String TAG = "abc";
static final int REQUEST_IMAGE_CAPTURE =1 ;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView)findViewById(R.id.myIV);
}
public void Capture(View v){
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,REQUEST_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
print("in onAC");
imageUri=data.getData();
iv.setImageURI(imageUri);
}
}
public void savee(View v){
Context context =this;
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File mydir = context.getDir("mydir", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, "myfile");
try {
FileOutputStream fos = new FileOutputStream(fileWithinMyDir);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
print("path is"+fileWithinMyDir.getAbsolutePath());
fos.flush();
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void saveFile(View v){
Context context =this;
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File mydir = context.getFilesDir();
String filename = "Arunachala/Shravan/Images/MyImage.jpg";
File file = new File(mydir, filename);
file.mkdirs();
print("path is"+file.getAbsolutePath());
try {
FileOutputStream fos = new FileOutputStream(file);
print("path is"+file.getAbsolutePath());
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void print(String s){
Log.d(TAG, s);
}
}
这是我的 content_main.xml 文件:
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.shravan.asbdbsadbsabcxscsa.MainActivity"
tools:showIn="@layout/activity_main">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myIV"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="175dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"
android:id="@+id/myB"
android:onClick="Capture"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/myIV" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/myB"
android:onClick="savee" />
</RelativeLayout>
这是我的 AndroidManifest.xml 文件:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shravan.asbdbsadbsabcxscsa">
<uses-feature android:name="android.hardware.Camera" android:required="true"></uses-feature>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我最近的logcat:
08-09 14:28:38.889 12727-12727/com.example.shravan.asbdbsadbsabcxscsa D/abc: 路径是/data/user/0/com.example.shravan.asbdbsadbsabcxscsa/app_mydir/myfile
请帮忙!
成功了:
保存代码:
FileOutputStream outStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/camtest");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
refreshGallery(outFile);
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
最后:
转到设备设置>设备>应用程序>应用程序管理器>"your app">权限>启用存储权限!
我一直在参考各种帖子和网站,我已经尝试了 3 天,但我无法弄清楚!
我的 Activity 有 2 个按钮!第一个是捕获,第二个是保存。按下捕获按钮后,我需要启动相机,然后当我们单击图片并单击确定时,图像将设置到 activity 中的 ImageView。现在,当我单击保存按钮时,我需要将图像保存在内部存储器 中我自己的文件夹中,该文件夹必须出现在文件资源管理器中 。说我的自定义文件夹>图片>MyCapture.jpg
我尝试了几个示例,但在一个案例中我能够做的是将图像存储在默认目录中,例如下载或 DCIM 或图片。
在其他情况下,我能够将图片存储在 Android>data>com.example.shravan.camera>files>Pictures>myImage.jpg
如果我在这里发帖,我可以将图像保存在 /data/user/0/com.example.shravan.asbdbsadbsabcxscsa/app_mydir/myfile
这里我还有另一种方法 saveFile ,因为我一如既往地收到 FileNotFound 异常 ,所以正在评论它!
但是,我无法保存在我自己的自定义文件夹中。请检查代码并帮助我,如果我遗漏了什么请告诉我!
代码:
MainActivity.java
package com.example.shravan.asbdbsadbsabcxscsa;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Uri imageUri;
private static final String TAG = "abc";
static final int REQUEST_IMAGE_CAPTURE =1 ;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView)findViewById(R.id.myIV);
}
public void Capture(View v){
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,REQUEST_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
print("in onAC");
imageUri=data.getData();
iv.setImageURI(imageUri);
}
}
public void savee(View v){
Context context =this;
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File mydir = context.getDir("mydir", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, "myfile");
try {
FileOutputStream fos = new FileOutputStream(fileWithinMyDir);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
print("path is"+fileWithinMyDir.getAbsolutePath());
fos.flush();
fos.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void saveFile(View v){
Context context =this;
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = drawable.getBitmap();
File mydir = context.getFilesDir();
String filename = "Arunachala/Shravan/Images/MyImage.jpg";
File file = new File(mydir, filename);
file.mkdirs();
print("path is"+file.getAbsolutePath());
try {
FileOutputStream fos = new FileOutputStream(file);
print("path is"+file.getAbsolutePath());
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void print(String s){
Log.d(TAG, s);
}
}
这是我的 content_main.xml 文件: content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.shravan.asbdbsadbsabcxscsa.MainActivity"
tools:showIn="@layout/activity_main">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myIV"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="175dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"
android:id="@+id/myB"
android:onClick="Capture"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/myIV" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/myB"
android:onClick="savee" />
</RelativeLayout>
这是我的 AndroidManifest.xml 文件: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shravan.asbdbsadbsabcxscsa">
<uses-feature android:name="android.hardware.Camera" android:required="true"></uses-feature>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我最近的logcat:
08-09 14:28:38.889 12727-12727/com.example.shravan.asbdbsadbsabcxscsa D/abc: 路径是/data/user/0/com.example.shravan.asbdbsadbsabcxscsa/app_mydir/myfile
请帮忙!
成功了:
保存代码:
FileOutputStream outStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/camtest");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
refreshGallery(outFile);
权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
最后:
转到设备设置>设备>应用程序>应用程序管理器>"your app">权限>启用存储权限!