【问题标题】:Save the image from an ImageView(captured from camera) in my custom defined folder in the INTERNAL MEMORY将 ImageView 中的图像(从相机捕获)保存在内部存储器中我自定义的文件夹中
【发布时间】:2016-08-09 19:42:38
【问题描述】:

我一直在参考各种帖子和网站,我从 3 天以来一直在尝试这个,但我无法弄清楚!

我的活动有 2 个按钮!第一个是捕获,第二个是保存。在按下捕获按钮时,我需要启动相机,然后当我们单击图片并单击 OK。时,图像被设置为 Activity 中的 ImageView。现在,当我点击保存按钮时,我需要将图像保存在内存中我自己的文件夹中,该文件夹必须出现在文件资源管理器中。说 MyCustomFolder>Pictures>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

请帮忙!

【问题讨论】:

  • "我需要将图像保存在我自己的文件夹中的内存中,该文件夹必须出现在文件资源管理器中。说 MyCustomFolder>Pictures>MyCapture.jpg" -- 请不要这样做。这相当于说您的 Windows 程序需要在 C: 驱动器上拥有自己的自定义目录。
  • 好的!这不可能吗?每当我打开我的设备存储时,我们可以看到各种应用程序的文件夹,如 SnapChat、WhatsAPP 等。那么,这不可能吗?
  • 有可能。这很草率,就像过去将所有内容都转储到C:(例如,1995 年之后)的 Windows 程序的开发人员也很草率。除此之外,您的代码中没有任何内容与您似乎正在尝试编写的external storage 有任何关系。你想Environment.getExternalStorageDirectory(),大概。另请注意,Android 区分大小写 - 将 android.hardware.Camera 替换为 android.hardware.camera
  • @CommonsWare 非常感谢您的回复!所以,现在如果我想在 Android>data>com.example.xyz>MyFolder>image.jpg 中有我的文件夹;我必须如何进行?我总是收到 FileNotFound 异常,在这种情况下,我的图像被保存到一些数据>用户>0> ..我在文件资源管理器中找不到!
  • 我也试过了,但是没用:public void saveImage(Context context, Bitmap ImageToSave){ String file_path= Environment.getExternalStorageDirectory().getAbsolutePath()+NameOfFolder;字符串 CurrentDateAndTime = getCurrentDateAndTime();文件目录 = 新文件(文件路径); dir.mkdirs();文件 file = new File(dir, NameOfFile + CurrentDateAndTime + ".jpg"); FileOutputStream fout = new FileOutputStream(file); ImageToSave.compress(Bitmap.CompressFormat.JPEG, 85, fout); fout.flush(); fout.close(); }

标签: android android-studio android-imageview filenotfoundexception internal-storage


【解决方案1】:

搞定了:

保存代码:

    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" />

最后:

转到设备设置>设备>应用程序>应用程序管理器>“您的应用”>权限>启用存储权限!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2015-09-07
    • 2014-05-16
    相关资源
    最近更新 更多