为了使我的图像自动显示在图库中,我使用了MediaScannerConnection
scanfile
方法。
代码如下:
package com.example.writeonscreen;
import android.support.v7.app.ActionBarActivity;
import android.util.AttributeSet;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.icu.text.SimpleDateFormat;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class WriteOnScreenActivity extends ActionBarActivity {
private String state;
Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TouchEventView(this,null));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.write_on_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.savepic) {
state=Environment.getExternalStorageState();
if(state.equals(Environment.MEDIA_MOUNTED)){
String fileJPG = null;
String datetime = new Date().toGMTString();
datetime = datetime.replace(" ", "");
datetime = datetime.replace(":", "");
final File path =
Environment.getExternalStoragePublicDirectory
(
//Environment.DIRECTORY_PICTURES
//Environment.DIRECTORY_DCIM
Environment.DIRECTORY_DCIM + "/Utkarshpic/"
);
// Make sure the Pictures directory exists.
if(!path.exists())
{
path.mkdirs();
}
final File file = new File(path, fileJPG +datetime+ ".jpg");
try
{
final FileOutputStream fos = new FileOutputStream(file);
final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);
//bmp.compress(CompressFormat.JPEG, 100, bos);
bmp.compress(CompressFormat.JPEG, 85, bos);
Toast.makeText(getBaseContext(), "Picture saved in Utkarshpic", Toast.LENGTH_SHORT).show();
bos.flush();
bos.close();
MediaScannerConnection.scanFile(getBaseContext(),new String[] {path.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Updated", Toast.LENGTH_SHORT).show();
}
});
}
catch (final IOException e)
{
e.printStackTrace();
}
}else{
Toast.makeText(getBaseContext(), "Cannot Save Image.No SD Card !", Toast.LENGTH_LONG).show();
}
return true;
}
return super.onOptionsItemSelected(item);
}
public class TouchEventView extends View {
Paint paint=new Paint();
Path path=new Path();
Bitmap myBitmap;
public TouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawPath(path, paint);
myBitmap=Bitmap.createBitmap((int)canvas.getWidth(), (int)canvas.getHeight(), Config.ARGB_8888);// bitmap created
canvas=new Canvas(myBitmap);
canvas.drawARGB(255, 255, 255,255);
canvas.drawPath(path, paint);
pass(myBitmap);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
float xPos=event.getX();
float yPos=event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
path.moveTo(xPos, yPos);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(xPos, yPos);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
invalidate();
return true;
}
}
public void pass(Bitmap myBitmap) {
// TODO Auto-generated method stub
bmp=myBitmap;
}
}
但是,当我运行该应用程序并保存图像然后转到图库时,看不到保存的图像。有人可以找出问题吗?
最佳答案
更改
MediaScannerConnection.scanFile(getBaseContext(),new String[] {path.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener()
从
getBaseContext()
到this
:-)
关于java - 媒体扫描仪连接未更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39449605/